Solutions to Midterm

Solutions to Midterm


  1. (35 points) Consider the following C++ program: ...
    
    
    1. (5 points) What does it output?
      
      30
      
      
    2. (5 points) Write a description of this program, as you would put in its header comment. Do NOT include the author or dates that normally appear in the header comment.
      
      The program computes the sum of the even integers between
      0 and 10, inclusive.  The result is ouput to the terminal
      
      
      
      
    3. (5 points) When the program is run and reaches line 18, what is the value of the variable i at that point?
      
      11
      
      
    4. (5 points) Suppose line 11 were changed to
      while (i < 10) {
      
      What effect would this have on the program?
      
      The program would output 20 instead of 30 [since 10 would
      not be added.]
      
      
      
      
      
    5. (5 points) Suppose line 11 were changed to
      while (i >= 10) {
      
      What effect would this have on the program?
      
      The program would output 0 [since the loop would fail
      immediately].
      
      
      
      
      
    6. (5 points) Suppose line 16 were changed to
      i++;
      
      (and line 11 is in its original form "while (i <= 10)"). What effect would this have on the program?
      
      No change [since i++ is equivalent to i = i + 1].
      
      
      
      
      
      
    7. (5 points) Suppose line 16 were removed entirely from the program (and line 11 is in its original form). What effect would this have on the program?
      
      The program would go into an infinite loop [since i would be
      stuck at 0].
      
    
    
  2. (65 points) Write a C++ program per the specification given below. ...
    #include <fstream.h>
    #include <iomanip.h>
    #include <iostream.h>
    
    int main() {
        float balance;                      // Initial and ending balance
        float check_amount;                 // Amount of each check
        int num_checks = 0;                 // Number of checks read
        ifstream file;                      // Input file
    
        //
        // Set up floating point output format.  (NOT REQUIRED in student answer.)
        //
        cout.setf(ios::fixed, ios::floatfield);
        cout.setf(ios::showpoint);
    
        //
        // Prompt for and read the initial balance.
        //
        cout << "Input the initial balance: ";
        cin >> balance;
    
        //
        // Open checks file and read the first check amount.
        //
        file.open("checks");
        file >> check_amount;
    
        //
        // Process the remaining checks in the file, decrementing the balance and
        // incrementing the check counter as each is read.
        //
        while (file) {
            balance = balance - check_amount;
            num_checks = num_checks + 1;
            file >> check_amount;
        }
    
        //
        // Output the results.
        //
        cout << num_checks << " checks read.  "
             << "Final balance is "
             << setprecision(2) << balance
             << endl;
    }