Part I - True/False 1. The + operator comes before the / operator in the order of operations. FALSE 2. Keywords are words that you cannot use as identifiers. TRUE 3. A bug is a logic error or other problem in a computer program. TRUE 4. "ant eater" is a legal identifier. FALE 5. The statement i++; increments the variable i. TRUE 6. The statement cin >> cost; gets a number from the keyboard and stores it in a variable named cost. TRUE 7. An if statement must have an else part. FALSE 8. A do while loop should be used when you can determine the exact number of iterations that will be necessary. FALSE 9. A break statement causes a loop to end. TRUE 10. A while loop is a top-checking, determinate loop. FALSE, it is top-checking but it is indeterminate 11. The loop set up with the statement for (j = 1; j <= 5; j++) will iterate exactly 5 times (assuming j is not affected within the loop and that there are no break's). TRUE 12. The loop set up with the statement for (j = 0; j < 10; j++) will iterate exactly 10 times (assuming j is not affected within the loop and that there are no break's). TRUE 13. The loop set up with the statement for (j = 10; j < 100; j = j + 3)will iterate exactly 5 times (assuming j is not affected within the loop and that there are no break's). FALSE 14. At one point the value of j is 11 when the loop set up with the statement for (j = -4; j <=20; j = j + 5) iterates. TRUE Part II - Write valid, efficient C++ code to perform the following tasks. It is NOT necessary to include documentation. However, you MUST use the same exact variable names that are specified. In each separate exercise, you may assume that the necessary variables have been declared unless you are being asked to write an actual declaration statement or a whole program. Use good white space and indentation, so that your answers are neat and easy to read. 1. Write a statement that declares a variable sum of type double. double sum = 0.0; 2. Write a statement that assigns the value 209 to the double variable sum. sum = 209; 3. Write a statement that displays the value of the integer variable sum to the screen. cout << sum; 4. Write a statement that increments the variable sum using the incrementing operator. sum++; 5. Write a single statement that declares a constant named DOZEN with the value of 12. Use a logical & reasonable data type. const int DOZEN = 12; 6. Write an if statement that displays the message "Won" if the value of the double variable runsScored is greater than 8. if ( runsScored > 8) { cout << "Won"; } 7. Write an if else statement that displays the message "You earned an A." if the value of the variable named studentGrade is greater than or equal to 90. Otherwise, the if else statement causes the message "You could have done better!" to display. if ( studentGrade >= 90) { cout << "You earned an A."; } else { cout << "You could have done better!"; } 8. Write a for loop that displays the values of 1 to 10 on the screen. for(int i = 1; i < 11; i++) { cout << i << endl; } 9. Write a for loop that counts by two’s from 10 to 20, displaying each value as it counts. for(int i = 10; i < 21; i = i + 2) { cout << i << endl; } 10. Write a top-checking, indeterminate loop that allows the user to input a sequence of integers until the sum of the numbers that he has entered is greater than 100. int sum = 0; int input = 0; while(sum <= 100) { cin >> input; sum += input; } 11. Write a whole program that prompts the user to enter his age. The program then displays the number of years the user has until retirement assuming that the user works until he is 65. int main() { const int RETIRE = 65; // retirement age int age = 0; // user's age int time = 0; // calculated time cout << "Input current age: "; cin >> age; time = RETIRE - age; cout << "You have " << time << " years until you can retire at age " << RETIRE << "."; system("pause"); return 0; }//end main 12. Write a full C++ program that obtains three floating-point values from the user via the keyboard, providing suitable prompt statements indicating that each input is a weight in pounds known to the hundredths place. The program must display the average of the three values rounded to the first decimal place (tenths place) along with a suitable message indicating that this result is the average. int main() { double num1; //weight 1 double num2; //weight 2 double num3; //weight 3 double avg; //average of inputs cout << "Input weight 1 to the hundredths place: "; cin >> num1; cout << "Input weight 2 to the hundredths place: "; cin >> num2; cout << "Input weight 3 to the hundredths place: "; cin >> num3; num1 = (int ((num1 * 100) + 0.5))/ 100.0; num2 = (int ((num2 * 100) + 0.5))/ 100.0; num3 = (int ((num3 * 100) + 0.5))/ 100.0; avg = (num1 + num2 +num3)/3; avg = (int ((avg * 100) + 0.5))/ 100.0; cout << "The average is: " << avg; system("pause"); return 0; }//end main 13. Draw a complete truth table with the rows and columns labeled exactly as I have done in the lecture notes. | a | b | && | || | | 0 | 0 | 0 | 0 | | 0 | 1 | 0 | 1 | | 1 | 0 | 0 | 1 | | 1 | 1 | 1 | 1 |