Name: _______Key________
 
Quiz 5 (Week 6)
CS-150, Fall ‘02, Dr. C. S. Tritt
 
Each question is worth the same amount.
 
1. What output would the following fragment of C++ code produce (assume any necessary header files have been included and variables declared and defined)?
 
   int x = 3;
   do
   {
      cout << x << endl;
      x = 2*x;
   }
   while (x <= 50);
   cout << "All done.\n";
 
Answer –
 
3
6
12
24
48
All
done.
 
2. Write a fragment of C++ code that displays a table (list) of 10 squared integer values. The code should prompt the user to enter a starting value. It should then read the value and display a sequence of values and their squares from the starting value up to and including the starting value +9. For example, if the user were to enter 4, the following table would be displayed (the exact format of the table is not important):
 
Enter a starting value: 4
4 16
…
13 169
 
Answer –
 
cout << "Enter
a starting value: ";
int start;
cin >> start;
for (x = start; x <=
start + 9; x++) cout << x << ' ' << x*x << 
endl;