CS 215 Section 401 Home work # 1 answers Solution R2.1: The value of mystery is equal to 0 after the statements are executed. In the first statement (line 1), mystery is initialized to a value of 1. In the assignment statement on line 2, mystery is set to -1. Finally, mystery is set to 0 in line 3. Solution R2.2: The variable mystery is being declared twice, first in line 1 and then again in line 2. A variable can only be initialized once. (If you remove the reserved word int on line 3, the statements will work just fine, and will set mystery to -3.) Solution R2.4: The C++ statements can be written as mathematical expressions as follows: a. 1 v / c d m m 1 1 v / c b. volume r 2 h volume c. d. 4 r 3 3 z x2 y2 Solution R2.8: Find at least 4 run-time errors in the program (line numbers added for reference): 1) 2) 3) 4) 5) 6) 7) 8) 9) 10) 11) 12) 13) 14) 15) 16) 17) 18) 19) #include <iostream> using namespace std; int main() { int total; int x1; cout << "Please enter a number:"; cin >> x1; total = total + x1; cout << "Please enter another number:"; int x2; cin >> x2; total = total + x1; double average = total / 2; cout << "The average of the two numbers is " << average << "endl"; return 0; 20) } Logic Errors: 1. The prompt does not specify an integer; if a number with a decimal part is entered, the decimal part will be truncated. 2. The total variable should be initialized to zero before being used on Line 11. 3. The variable x1 was mistakenly used instead of x2 on Line 15. 4. Since the variable total was defined to be an integer, the division on Line 16 will be treated as an integer division, and digits after the decimal point in the result will be truncated. 5. There should not be double quotes around endl on Line 18. Solution R2.19: When the program runs, it first prompts the user to enter a number. It stores this number in x1 and then tries to add x1 to the total variable. When the program hits the line where the total variable is first used, an error message is displayed stating that the total variable is being used before it was initialized. If the debugger allows you to continue, the value that is displayed for total (the first cout line) is based on whatever random value was in the total variable when the program started. (See figure below for example output.) The second error occurs because the integer variable total is used to compute and hold the average. This is misleading to the programmer as well as the user of the program (because a variable called total is holding the sum). Also, the total variable should not be an integer, since the average will very likely not be an integer number (and storing a non-integer result in an integer variable will result in loss of information). The first error in the program can be easily fixed by initializing the total variable to 0 when the variable is first declared. The second error is fixed by declaring a second value called average and using that instead of the variable total near the end of the program. Solution R2.21: Recommendations for using variables and constants: Use descriptive variables names (for example, use ounces_per_can instead of simply x or opc). Initialize variables before they are used. The names of constants should always be UPPERCASE to distinguish them from regular variables. Use a constant to help explain the meaning of numeric values (for example, using PENNIES_PER_DOLLAR instead of simply using the number 100). Constants should be declared at the top of the function in which they are used. Variables should only be declared just before they are needed. Use comments near variables and constants to provide an explanation of what the value is or how it is used. Solution R3.3: Example code fragment: if (x > 0) { y = x; } else { y = 0; } Solution R3.15: The pseudocode to print a message when it is one of four special holidays. Prompt user to enter a month and day. If month = January and day = 1 Print “New Year’s Day” Else if month = July and day = 4 Print “Fourth of July” Else if month = November and day = 11 Print “Veteran’s Day” Else if month = December and day = 25 Print “Christmas Day” Else print “Not one of the holidays.” Solution R3.25: True. Both A and B have to be true in order for the Boolean expression to be true. In C++ terms, this means that both A and B have to be evaluated anyway to evaluate the expression, so reversing the order of A and B has no effect on the outcome or on the efficiency of the computation. Solution R3.27: The Boolean expressions evaluate as follows (zero evaluates to false): a. b. c. d. e. f. g. h. a. b. c. d. e. f. g. h. false true true true false false false true Solution R4.2: The loops print as follows: a. b. c. d. e. f. 1 2 3 4 5 6 7 8 9 1 3 5 7 9 10 9 8 7 6 5 4 3 2 0 1 2 3 4 5 6 7 8 9 1 2 4 8 2 4 6 8 Solution R4.7: How often do the loops execute? a. b. c. d. e. f. g. The loop executes 10 times. The loop executes 10 times. The loop executes 10 times. The loop executes 21 times. This is an infinite loop. This loop executes 11 times. The loop executes 7 times. Solution R4.12: Because a do loop will always execute at least once, to rewrite this code as a while loop, we can simply copy the code inside the loop and place it up above the while loop. The loop rewritten as a while loop would look like this: int n; cin >> n; // “Extra” copy of loop contents double s = 1.0 / (1 + n * n); n++; // “x” will be zero to start, so just set x = s double x = s; while (s > 0.01) { s = 1.0 / (1 + n * n); n++; x = x + s; } Solution R4.23: One possible implementation of a single-loop version of the code is as follows: int count = 0; for (int i = 1; i <= height * width; i++) { if (count == width) { count = 1; cout << endl; } else { count++; } cout << "*"; }