Assignment 2 consists of FOUR (4) exercises: Solved

$24.99 $18.99

Using Constant Variables Instead of Macros in C++ Although macros are supported in both C and C++, constant variables (or inline functions if necessary) are preferred in C++ except in the few situations where only a macro can do the job. The C and C++ keyword const and the C++-only keyword constexpr are known as…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Description

5/5 – (2 votes)

Using Constant Variables Instead of Macros in C++

Although macros are supported in both C and C++, constant variables (or inline functions if necessary) are preferred in C++ except in the few situations where only a macro can do the job.

The C and C++ keyword const and the C++-only keyword constexpr are known as type qualifiers. Using either of them to declare a variable makes it a constant variable. A constant variable must be explicitly initialized when declared and its value cannot be changed thereafter. That is, it is “read only”.

In C++, a constant variable that is initialized with a constant expression can be used in contexts where a constant expression is required, such as for dimensioning arrays, specifying case values in switch statements, specifying bit-field widths, and initializing enumerators. A constant expression is an expression that can be fully evaluated at compile time rather than having to wait for the program to run. In C, a constant variable can never be used where a constant expression is required even if its initializer is a constant expression.

const or constexpr

In C++, the most common practice is to use the constexpr qualifier when the variable must be useable in situations where a constant expression is required, such as those mentioned above. If you then mistakenly try to initialize the variable with a non-constant expression, the compiler will generate an error so you can fix the problem. It is also a common practice to specify constexpr any time the initializer is a constant expression even if the variable will not be used in a situation that requires one.

Examples:

#define NEW_STUDENTS 15

// Avoid an inappropriate magic number in C

constexpr int NEW_STUDENTS = 15;

// Avoid an inappropriate magic number in C++

constexpr double SQRT = sqrt(20);

//

Error – initializer is not a constant expression

const double SQRT = sqrt(20);

//

Okay

1992-2023 Ray Mitchell Page 2 of 3 of C1A2 General Information

Personalized C1A2 requirements exclusively for Jose Medrano (U09845800)

C/C++ Programming I (Section 174273)

C1A2 General Information, continued

Limiting the “Scope” of Variables

The scope of an identifier (a name) is defined as the section of code over which it is accessible. The scope of a variable declared inside a function extends from that declaration to the end of the block in which it is declared. Good programming practice dictates that the scopes of non-const variables be as small as possible to prevent their values from being changed by code that should not change them. Consider the examples below. Constant variables that are being used in C++ instead of macros are exceptions. For readability these are often defined in the same place the equivalent macros would have been defined if it were C code. Otherwise, they should be defined first in the function or block that uses them.

Personalized C1A2 requirements exclusively for Jose Medrano (U09845800)

C/C++ Programming I (Section 174273)

C1A2E0 (6 points total – 1 point per question – No program required)

Assume language standards compliance and any necessary standard library support unless stated otherwise. These are not trick questions and there is only one correct answer. Basing an answer on actual testing is risky. Place your answers in a plain text “quiz file” named C1A2E0_Quiz.txt formatted as:

a “Non-Code” Title Block, an empty line, then the answers:

  1. A

  2. C etc.

Personalized C1A2 requirements exclusively for Jose Medrano (U09845800)

C/C++ Programming I (Section 174273)

  1. C1A2E1 (5 points – C++ Program)

  1. Exclude any existing source code files that may already be in your IDE project and add a new one,

  2. naming it C1A2E1_main.cpp. Write a program in that file to convert a lowercase character to

  1. uppercase.

5

  1. Your program must:

  2. 1. Prompt (ask) the user to enter any character.

  3. 2. Use cin.get to read the character.

  4. 3. Assume the input character is lowercase (even if it isn’t) and attempt to convert it to uppercase.

  5. 4. Display the results in the following format with single quotes around the original and converted

  6. characters, respectively, followed by the underlying decimal value of the converted character.

  7. For example, if the user inputs the character a, the output must be exactly as shown below:

13 The uppercase equivalent of ‘a’ is ‘A’ (decimal value = 65)

  1. 5. Not test anything; simply apply the same conversion algorithm to any input character without

  2. regard for whether it was actually lowercase and display the results.

  3. 6. Not use toupper or any other function to do the conversion (although toupper is the best

  4. solution in “real life”).

  5. 7. Not name any variable uppercase (to avoid standard library conflicts & a bogus assignment

  6. checker warning).

20

21

Manually re-run your program several times, testing with at least the following 6 input characters:

22

b z Q 0 } a literal space

23

  1. Explain what happens and why in the following two situations and place these explanations as

  2. comments in your “Title Block”:

  3. 1. The user enters anything other than a lowercase character.

  1. 2. The user precedes the input character with a whitespace.

28

29

  1. Submitting your solution

  1. `Send an empty-body email to the assignment checker with the subject line C1A2E1_174273_U09845800

  1. and with your source code file attached.

  1. See the course document titled “How to Prepare and Submit Assignments” for additional exercise

  2. formatting, submission, and assignment checker requirements.

35

36

  1. Hints:

  1. The “underlying value” of a character simply means the value used to represent that character in the

  2. character set being used. For example, in the ASCII character set (Note B.1) the underlying value of the

  3. character ‘@’ is 64 decimal (or if you prefer, 100 octal or 40 hexadecimal).

41

  1. The most general way to represent the numerical difference between the ASCII uppercase and

  2. lowercase character sets is the expression ‘a’ – ‘A’. Initialize a constant variable (Note 2.14) to that

  3. expression and use it in your code and comments as needed. Note, however, that the standard library

  4. function toupper provides the most portable solution, although you are not allowed to use it in this

  5. exercise. This function and its tolower counterpart will do the conversions in a completely portable

  1. way without regard for the specific characteristics of whatever character set is being used. For your

  2. own knowledge and for future use you should look up these two functions in your compiler’s

  3. documentation, in one of the books recommended for this course, or online.

1992-2021 Ray Mitchell Page 1 of 1 of C1A2E1

Personalized C1A2 requirements exclusively for Jose Medrano (U09845800)

C/C++ Programming I (Section 174273)

  1. C1A2E2 (5 points – C Program)

  1. Exclude any existing source code files that may already be in your IDE project and add a new one,

  2. naming it C1A2E2_main.c. Write a program in that file to display a triangle of characters.

4

  1. 1. Define two macros named LEADER_CHAR and DIAGONAL_CHAR. The replacement list of each must

  2. be an arbitrary character literal. Referring to those characters by name (such as “circumflex” and

  3. dollar”), by numeric value (such as 94 and 36), or by literal (such as ‘^’and ‘$’) anywhere else in

  1. the program, including in comments, is an inappropriate use of “magic numbers”.

  1. 2. Prompt (ask) the user to enter any positive decimal integer value.

  1. 3. Use a nested loop to display that number of lines of characters on the console screen starting in

  2. column 1, with each successive line containing one more character than the previous. Each line

  3. must end with the diagonal character and any preceding characters must be leader characters.

  1. The first line will only contain the diagonal character. For example, if the user inputs a 4 and the

  1. leader character is ^ and the diagonal character is $, the following will be displayed:

  2. $

  3. ^$

  1. ^^$

  2. ^^^$

  1. 4. DO NOT use more than two loop statements.

20

  1. Manually re-run your program several times, testing with several different line counts, leader characters,

  1. and diagonal characters. To change the leader and diagonal characters you must change the

  2. character literals associated with the LEADER_CHAR and DIAGONAL_CHAR macros and recompile.

24

  1. Submitting your solution

  1. `Send an empty-body email to the assignment checker with the subject line C1A2E2_174273_U09845800

  1. and with your source code file attached.

  1. See the course document titled “How to Prepare and Submit Assignments” for additional exercise

  2. formatting, submission, and assignment checker requirements.

30

31

  1. Hints:

  1. 1. You are unnecessarily complicating your code if you use any “if” statements or more than three

  2. variables.

  1. 2. A “nested loop” is merely a loop of any kind that is within the body of another loop of any kind, as

  2. in the example below. In this exercise the outer loop would be used to keep track of the number

  3. of lines and the inner loop would be used to keep track of the number of leader characters on

  4. each line. Use a “for” loop if a variable must be initialized when the loop is first entered, tested

  5. before each iteration, and updated after each iteration. Choose meaningful names for loop

  6. count variables. Names like i, j, k, outer, inner, loop1, loop2, counter, etc., are not

  7. informative and are usually inappropriate.

42

for (…)

43

44

{

45

for (…)

46

{

47

48

}

  1. }

1992-2023 Ray Mitchell Page 1 of 1 of C1A2E2

Personalized C1A2 requirements exclusively for Jose Medrano (U09845800)

C/C++ Programming I (Section 174273)

  1. C1A2E3 (4 points – C++ Program)

  1. Exclude any existing source code files that may already be in your IDE project and add a new one,

  2. naming it C1A2E3_main.cpp. Write a program in that file to display a triangle of characters.

4

  1. 1. Declare two constexpr-qualified type char variables named LEADER_CHAR and DIAGONAL_CHAR in

  1. two separate statements on two separate lines. As part of each statement, initialize the variable to

  2. an arbitrary character literal. Referring to those characters by name (such as “circumflex” and

  1. dollar”), by numeric value (such as 94 and 36), or by literal (such as ‘^’and ‘$’) anywhere else in

  1. the program, including in comments, is an inappropriate use of “magic numbers”.

  1. 2. Prompt (ask) the user to enter any positive decimal integer value.

  1. 3. Use a nested loop to display that number of lines of characters on the console screen starting in

  2. column 1, with each successive line containing one more character than the previous. Each line

  1. must end with the diagonal character and any preceding characters must be leader characters.

  2. The first line will only contain the diagonal character. For example, if the user inputs a 4 and the

  1. leader character is ^ and the diagonal character is $, the following will be displayed:

  2. $

  1. ^$

  2. ^^$

  3. ^^^$

  1. 4. DO NOT use more than two loop statements.

21

  1. Manually re-run your program several times, testing with several different line counts, leader characters,

  2. and diagonal characters. To change the leader and diagonal characters you must change the

  3. character literals assigned to the LEADER_CHAR and DIAGONAL_CHAR variables and recompile.

25

  1. Submitting your solution

  1. `Send an empty-body email to the assignment checker with the subject line C1A2E3_174273_U09845800

  1. and with your source code file attached.

  1. See the course document titled “How to Prepare and Submit Assignments” for additional exercise

  2. formatting, submission, and assignment checker requirements.

31

32

  1. Hints:

  1. 1. You are unnecessarily complicating your code if you use any “if” statements or more than five

  2. variables.

  1. 2. A “nested loop” is merely a loop of any kind that is within the body of another loop of any kind, as

  2. in the example below. In this exercise the outer loop would be used to keep track of the number

  3. of lines and the inner loop would be used to keep track of the number of leader characters on

  4. each line. Use a “for” loop if a variable must be initialized when the loop is first entered, tested

  1. before each iteration, and updated after each iteration. Choose meaningful names for loop

  2. count variables. Names like i, j, k, outer, inner, loop1, loop2, counter, etc., are not

  3. informative and are usually inappropriate.

43

for (…)

44

45

{

46

for (…)

47

{

48

49

}

  1. }

1992-2023 Ray Mitchell Page 1 of 1 of C1A2E3

Page 7 (7/11/2023)

Assignment 2 consists of FOUR (4) exercises: Solved
$24.99 $18.99