Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [10pts] Consider the following program, which is supposed to compute the sum of the integers from 1 to 10.
    
    #include <iostream>
    using namespace std;
    int main()
    {
      int i = 1;
      while(i <= 10)
      {
        int sum = 0;
        sum = sum + i;
      }
      cout << sum << endl;
      return 0;
    }
    1. The program doesn't compile. What kind of error messages will you get from the compiler?
      
      
      
      
      
    2. Annotate the above code fix this error (and any other errors) so that the program both compiles and works properly.
  2. [10pts] Consider the following program. What will be the output? Try to solve it manually instead of compiling and running the code. We will ask this kind of question in the exam.
    
    int a = 1, b = 2;
    
    while(a <= 10)
    {
      if( a % 2 ) 
      {
         a = a + b;
         b = b + 2;
      }
      else
      {
         a = a + 1;
         b = b - 1;
      }
    
      cout << a << " " << b << endl;
    }
    
    
    output:
  3. [10pts] Consider the following code:
    
    ifstream fin("foo.txt");
    int count = 0;
    char c = 'x';
    while(fin)
    {
    	if (c == '?')
     		count = count + 1;
    	fin >> c;
    }
    cout << count << endl;
    
    1. What would normally cause the program to exit the while loop?
      
      
      
      
    2. What exceptional condition would cause the program to never enter the while loop?
      
      
      
      
  4. [70pts] Write a program hw.cpp that works as follows:
    1. It reads in a file that contains the names of several students along with their hw, quiz and exam averages, e.g., input.txt.
    2. For each student, it prints out the student's name and the overall average score.
    3. When computing the average, use the following weights:
      hw: 20%, quiz: 20%, exam: 60%.
    4. In the end, the program shows the best student.
    With input.txt, an example run is given below (with user input colored red):
    ~$ ./hw
    Filename: input.txt
    Brown   71.8
    Green   93.8
    Jones   82.4
    Smith   86.2
    Worth   91.2
    The best student is Green. 
    

    Your program should work for any file in the same format. Another example run with test.txt:

    ~$ ./hw
    Filename: test.txt
    Abel	85.4
    Dean	88.8
    Jade	84
    Mood	78.2
    Noon	83.8
    Wren	62
    Zoom	85.2
    The best student is Dean.