



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Chapter 7 Self Test Exercises
32.For each of the following situations, tell which type of loop(while, do-while, or for) would work best: a) Summing a series, such as 1/2 + 1/3 + 1/4 + 1/5 + … + 1/10. b) Reading in the list of exam scores for one student. c) Reading in the number of days of sick leave taken by employees in a department. d) Testing a function to see how it performs for different values of its arguments. 33.Rewrite the following loops as for loops: a. int i = 1; while ( i <= 10) { if (i < 5 && i != 2) cout << ‘X’; i++; } b. int i = 1; while (i <= 10) { cout << ‘X’; i = i + 3; } c. long m = 100; do { cout << ‘X’; m = m + 100; } while (m < 1000); 34.What is the output of this loop? Identify the connection between the value of n and the value of the variable log. int n = 1024; int log = 0; for (int i = 1; i < n; i = i * 2) log++; cout << n << “ “ << log << endl; 35.What is the output of this loop? Comment on the code. int n = 1024; int log = 0; for (int i = 1; i < n; i = i * 2) log++; cout << n << “ “ << log << endl; 36.What is the output of this loop? Comment on the code. int n = 1024; int log = 0; for (int i = 0; i < n; i = i * 2) log++; cout << n << “ “ << log << endl;
Chapter 7 Self Test Exercises (on sections that were skipped in Midterm 2) 17.Given the following declaration and output statement, assume that this has been embedded in a correct program and is run. What is the output? enum Direction { N, S, E, W }; //... cout << W << “ “ << E << “ “ << S “ “ << N << endl; 18.Given the following declaration and output statement, assume that this has been embedded in a correct program and is run. What is the output? enum Direction { N = 5, S = 7, E = 1, W }; //... cout << W << “ “ << E << “ “ << S “ “ << N << endl; 19.What output will be produced by the following code, when embedded in a complete program? int first_choice = 1; switch (first_choice + 1) { case 1: cout << “Roast beef\n”; break; case 2: cout <, “Roast worms\n”; break; case 3: cout << “Chocolate ice cream\n”; case 4: cout << “Onion ice cream\n”; break; default: cout << “Bon appetite!\n”; } 20.What would be the output in problem 19 above if the first line were changed to the following? int first_choice = 3; 21.What would be the output in problem 19 above if the first line were changed to the following? int first_choice = 2; 22.What would be the output in problem 19 above if the first line were changed to the following? int first_choice = 4;