| Tracing While Loops Worksheet #2 | Name - |
| Day of Week - |
Trace the following programs. Show the successive values stored in each variable in a column in the right margin. Assume the user enters the following values in this exact order: 1, 3, 1, 13, 1, 4, 2
1.
#include <iostream>
using namespace std;
const double BOOK_PRICE = 9.00;
int main()
{
int numBooks = 0;
int totalBooks = 0;
int userInput = 1;
double totalPrice = 0.0;
while (userInput == 1)
{
cout << "Enter 1 for more books or 2 to quit: ";
cin >> userInput;
if (userInput == 1)
{
cout << "Enter quantity of books: ";
cin >> numBooks;
if (numBooks > 0 && numBooks < 10)
{
totalBooks += numBooks;
}
}
}
totalPrice = totalBooks * BOOK_PRICE;
cout << totalPrice << endl;
return 0;
} // end of main
2.
#include <iostream>
using namespace std;
int main()
{
int debt = 10000;
int years = 0;
while (debt > 0 && years < 100)
{
debt = debt - 2000;
years++;
}
cout << years << endl;
return 0;
}// end of main