$24
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:
A
C etc.
Personalized C1A2 requirements exclusively for Jose Medrano (U09845800)
C/C++ Programming I (Section 174273)
C1A2E1 (5 points – C++ Program)
Exclude any existing source code files that may already be in your IDE project and add a new one,
naming it C1A2E1_main.cpp. Write a program in that file to convert a lowercase character to
uppercase.
5
Your program must:
1. Prompt (ask) the user to enter any character.
2. Use cin.get to read the character.
3. Assume the input character is lowercase (even if it isn’t) and attempt to convert it to uppercase.
4. Display the results in the following format with single quotes around the original and converted
characters, respectively, followed by the underlying decimal value of the converted character.
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)
5. Not test anything; simply apply the same conversion algorithm to any input character without
regard for whether it was actually lowercase and display the results.
6. Not use toupper or any other function to do the conversion (although toupper is the best
solution in “real life”).
7. Not name any variable uppercase (to avoid standard library conflicts & a bogus assignment
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
Explain what happens and why in the following two situations and place these explanations as
comments in your “Title Block”:
1. The user enters anything other than a lowercase character.
2. The user precedes the input character with a whitespace.
28
29
Submitting your solution
`Send an empty-body email to the assignment checker with the subject line C1A2E1_174273_U09845800
and with your source code file attached.
See the course document titled “How to Prepare and Submit Assignments” for additional exercise
formatting, submission, and assignment checker requirements.
35
36
Hints:
The “underlying value” of a character simply means the value used to represent that character in the
character set being used. For example, in the ASCII character set (Note B.1) the underlying value of the
character '@' is 64 decimal (or if you prefer, 100 octal or 40 hexadecimal).
41
The most general way to represent the numerical difference between the ASCII uppercase and
lowercase character sets is the expression 'a' - 'A'. Initialize a constant variable (Note 2.14) to that
expression and use it in your code and comments as needed. Note, however, that the standard library
function toupper provides the most portable solution, although you are not allowed to use it in this
exercise. This function and its tolower counterpart will do the conversions in a completely portable
way without regard for the specific characteristics of whatever character set is being used. For your
own knowledge and for future use you should look up these two functions in your compiler’s
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)
C1A2E2 (5 points – C Program)
Exclude any existing source code files that may already be in your IDE project and add a new one,
naming it C1A2E2_main.c. Write a program in that file to display a triangle of characters.
4
1. Define two macros named LEADER_CHAR and DIAGONAL_CHAR. The replacement list of each must
be an arbitrary character literal. Referring to those characters by name (such as “circumflex” and
“dollar”), by numeric value (such as 94 and 36), or by literal (such as '^'and '$') anywhere else in
the program, including in comments, is an inappropriate use of “magic numbers”.
2. Prompt (ask) the user to enter any positive decimal integer value.
3. Use a nested loop to display that number of lines of characters on the console screen starting in
column 1, with each successive line containing one more character than the previous. Each line
must end with the diagonal character and any preceding characters must be leader characters.
The first line will only contain the diagonal character. For example, if the user inputs a 4 and the
leader character is ^ and the diagonal character is $, the following will be displayed:
$
^$
^^$
^^^$
4. DO NOT use more than two loop statements.
20
Manually re-run your program several times, testing with several different line counts, leader characters,
and diagonal characters. To change the leader and diagonal characters you must change the
character literals associated with the LEADER_CHAR and DIAGONAL_CHAR macros and recompile.
24
Submitting your solution
`Send an empty-body email to the assignment checker with the subject line C1A2E2_174273_U09845800
and with your source code file attached.
See the course document titled “How to Prepare and Submit Assignments” for additional exercise
formatting, submission, and assignment checker requirements.
30
31
Hints:
1. You are unnecessarily complicating your code if you use any “if” statements or more than three
variables.
2. A “nested loop” is merely a loop of any kind that is within the body of another loop of any kind, as
in the example below. In this exercise the outer loop would be used to keep track of the number
of lines and the inner loop would be used to keep track of the number of leader characters on
each line. Use a “for” loop if a variable must be initialized when the loop is first entered, tested
before each iteration, and updated after each iteration. Choose meaningful names for loop
count variables. Names like i, j, k, outer, inner, loop1, loop2, counter, etc., are not
informative and are usually inappropriate.
42
for (...)
43
44
{
45
for (...)
46
{
47
...
48
}
}
1992-2023 Ray Mitchell Page 1 of 1 of C1A2E2
Personalized C1A2 requirements exclusively for Jose Medrano (U09845800)
C/C++ Programming I (Section 174273)
C1A2E3 (4 points – C++ Program)
Exclude any existing source code files that may already be in your IDE project and add a new one,
naming it C1A2E3_main.cpp. Write a program in that file to display a triangle of characters.
4
1. Declare two constexpr-qualified type char variables named LEADER_CHAR and DIAGONAL_CHAR in
two separate statements on two separate lines. As part of each statement, initialize the variable to
an arbitrary character literal. Referring to those characters by name (such as “circumflex” and
“dollar”), by numeric value (such as 94 and 36), or by literal (such as '^'and '$') anywhere else in
the program, including in comments, is an inappropriate use of “magic numbers”.
2. Prompt (ask) the user to enter any positive decimal integer value.
3. Use a nested loop to display that number of lines of characters on the console screen starting in
column 1, with each successive line containing one more character than the previous. Each line
must end with the diagonal character and any preceding characters must be leader characters.
The first line will only contain the diagonal character. For example, if the user inputs a 4 and the
leader character is ^ and the diagonal character is $, the following will be displayed:
$
^$
^^$
^^^$
4. DO NOT use more than two loop statements.
21
Manually re-run your program several times, testing with several different line counts, leader characters,
and diagonal characters. To change the leader and diagonal characters you must change the
character literals assigned to the LEADER_CHAR and DIAGONAL_CHAR variables and recompile.
25
Submitting your solution
`Send an empty-body email to the assignment checker with the subject line C1A2E3_174273_U09845800
and with your source code file attached.
See the course document titled “How to Prepare and Submit Assignments” for additional exercise
formatting, submission, and assignment checker requirements.
31
32
Hints:
1. You are unnecessarily complicating your code if you use any “if” statements or more than five
variables.
2. A “nested loop” is merely a loop of any kind that is within the body of another loop of any kind, as
in the example below. In this exercise the outer loop would be used to keep track of the number
of lines and the inner loop would be used to keep track of the number of leader characters on
each line. Use a “for” loop if a variable must be initialized when the loop is first entered, tested
before each iteration, and updated after each iteration. Choose meaningful names for loop
count variables. Names like i, j, k, outer, inner, loop1, loop2, counter, etc., are not
informative and are usually inappropriate.
43
for (...)
44
45
{
46
for (...)
47
{
48
...
49
}
}
1992-2023 Ray Mitchell Page 1 of 1 of C1A2E3
Page 7 (7/11/2023)