CMPSC 201 - Ch. 2 Notes
Objective #1: Identify and create valid C++ identifiers (function and variable
names.)
- see the rules on p. 35 of Bronson
- Use valid identifiers
(often just called "variable names")
- do not use keywords that are defined in the programming language
- do not include spaces or other disallowed characters
- do not use more than 31 characters
- begin the identifier with a letter
- Use a conventional method of making your variables easy to read at a quick
glance. For this class,
we will begin variable identifiers with lowercase letters (eg. grade). However,
if you wish to use more than one word within the identifier, you must capitalize
the following words or parts of words (eg. semesterGrade, billGatesWealth).
Please do not separate successive words
with underscore characters ( _ ) as you may have seen in the textbook or other
programming courses (eg. semester_grade, bill_gates_wealth).
Objective #2: Explain and appreciate the importance of using good C++ coding
style.
- Note the different forms of style (good and bad) on p. 45
- comments can be inline (using // ) or block ( using /*....*/ ). However,
I prefer that you use // comments on programming assignments. /* ... */ comments
should only be used while debugging a program.
- For this course, you will be required to follow our Coding
Standards. Otherwise, you will lose points on assignments even if a program
happens to execute successfully.
- Since it is extremely rare for a programmer to work alone, his or her code
must be read by coworkers from time to time. It is important to observe common
coding standards so that others can read & understand your code with minimum
difficulty. Also, others will need to integrate your code into their program,
so following consistent coding standards will help avoid syntax and lexical
errors.
Objective #3: List and explain the differences between the three basic data
types in C++: integers, floating-points, and characters.
For characters (that is a single letter, digit, or symbol):
- char (-128 to 127; 1 byte)
- unsigned char (0 to 255; 1 byte)
- Even though the float data type is used
by our textbook author to store decimal numbers and even though a float
variable only uses 4 bytes, you should use the double
data type in CMPSC 201.
You don't have to understand the following discussion but it may be interesting
to some of you. Decimal numbers (aka floating point numbers) are stored in
a binary version of scientific notation (b * m * 2^e) with one bit (b) allocated
to sign, some bits allocated to the mantissa (m) and the rest to the exponent
(e). Therefore a float may have less bits allocated for the mantissa than
for an int. In Visual C++, the mantissa for a float gets 23 bits when an int
gets 31. Also rounding errors occur when converting real numbers to and from
base 2. This combination sometimes results in a float that is slightly smaller
than the expected value. If that float is supposed to store an integer, the
truncation that results when it is assigned to an int often lowers the expected
value by 1. Therefore, to avoid this problem, use a double that stores 52
bit mantissas.
- use single quotes when delimiting characters (eg. 'a'). For example,
char letterGrade = 'A';
and not
char letterGrade = A;
- be familiar with the ASCII chart
and memorize the ASCII values for 'a' (97),
'A' (65), & a blank space (32)
Objective #4: Correctly evaluate C++ expressions and identify the data type
of their results.
Objective #5: Evaluate expressions appropriately with integer division.
- integer division truncates the results of expressions
- Do not assume that the compiler "rounds up".
- Example:
int amount = 9;
int numItems = 2;
int average = 0;
average = amount / numItems;
The final value stored in the variable average is 4, not 4.5, due to integer
division.
Objective #6: Use assignment statements with correct C++ syntax.
Objective #7: Use declaration statements with correct C++ syntax.
- Variables must be declared before they are used in C++. Get into
the habit of doing this at the top of your program.
- Examples of declaration statements with explanatory inline comments:
char grade; //
a students semester grade
unsigned long gatesWealth; // the
number of dollars in Bill Gates' savings account
bool flag;
// whether user wishes
to play the game again or not
- For this course, please do not initialize multiple variables in a single
statement with commas even though your textbook and other C++ programmers
may do so. Rather, you should use multiple statements for clarity and the
preferred style for this course.
Instead of
int num = 0, salary = 0;
or
int num = 0,
salary = 0;
please use
int num = 0;
int salary = 0;
- You should get into the habit of initializing
variables at the top of the program; otherwise "garbage values"
(indeterminate values)
may corrupt your program.
- Examples:
- int numberOfPizzas = 3; //
declaring the variable & initializing it to the value of 3
- double monthlyCarPayment; // this
unwisely only declares the variable
- true initialization occurs within variable declaration statements
only. An assignment statement that assigns the value of zero to a variable
towards the top of a program technically is not considered initialization.
- identifiers (i.e. variable names) are really just tags for memory addresses
similar to how the Web page address www.microsoft.com is a tag for an IP address.
- As an exercise, write a program that prints out the indeterminant value
of an uninitialized variable.
Objective #8: Use the endl manipulator correctly
- endl causes '\n' and it flushes the output stream
- '\n' is the newline escape sequence where \ is the escape operator
in C++
Objective #9: Explain the differences between using pseudocode and flowcharts
to developing a program's algorithm.
- pseudocode, see p. 21
- flowcharts, see pp. 12 & 21
- You will be responsible for turning in pseudocode with most if not all programming
assignments that you complete for this course. We will not be using flowcharts.
Objective #10: Use a rounding algorithm.
- The following algorithm, which I call the "macho rounding algorithm", is not demonstrated in your textbook. It uses a combination of type casting and promotion to permanently change a variable's stored value to a rounded value.
- The following statements are basically equivalent in that they round any positive value stored in the variable num to the the nearest whole number
num = int (num + 0.5);
num = floor (num + 0.5);
- The first example above uses type casting to truncate the decimal places of the value after 0.5 is added to it.
- The second example uses the floor function which causes positive and negative numbers in the following set of parentheses to be truncated. You must add #include <cmath> at the top of your program in order to use the floor function.
- The following versions of the 3 example statements round num to the nearest hundredth's place.
num = (int ((num * 100) + 0.5))/ 100.0;
num = (floor ((num * 100) + 0.5))/ 100.0;
- By first multiplying by 100 to move the decimal place two places to the right and then using type casting to truncate the decimal places, you are then set up to simply divide by 100.0 to move the decimal point two places back to the left.
- You must divide by 100.0 and not the whole number 100. Otherwise integer division will occur and you will lose the desired decimal digits. By using 100.0 instead of 100, you are taking advantage of promotion where the C++ compiler will promote the numerator int operand to a double in order to match the 100.0 denominator term which is a double.