Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [15pts] Consider the following source code:
    
    // using for loops
    #include <iostream>
    using namespace std;
    int main()
    {
      int a, b;
      cin >> a >> b;
      for(int i=1; i < a; i++)
      {
        for(int j=b; j >= 0; j -= 2)
          cout << i*j << " ";
    
        cout << endl;
      }
      return 0;
    }
    
    
    
    // using while loops
    
    1. [5pts] Suppose the user gives the following input to the program:
       3 3 
      Write below the output of the program, given the above input. Try it without compiling and running the code.
      
      
      
      
      
      
    2. [10pts] Rewrite the code so that the for-loops are replaced with while-loop loops. Use the empty space in the above right.

  2. [85pts] Write a program hw.cpp that works as follows:
    1. The program reads a text file specified by a user.
    2. The file contains an arbitrary number of integers each of which is between 10 and 49 (inclusive). Assume all the numbers are always in the correct range (i.e., between 10 and 49).
    3. After reading all the numbers, the program draws a histogram.
    A sample run is as follows:

    dataA.txt sample run
    48 
    13 
    39 
    30 
    37 
    44 
    
    ~/$ ./hw
    Enter a filename: dataA.txt
    [10,19]: *
    [20,29]: 
    [30,39]: ***
    [40,49]: **
    

    In the above output,

    We provide three files: dataA.txt, dataB.txt, and dataC.txt. Below, we give a sample run for each of dataB.txt and dataC.txt.

    ~/$ ./hw
    Enter a filename: dataB.txt
    [10,19]: ********
    [20,29]: ****
    [30,39]: *******
    [40,49]: *
    
    ~/$ ./hw
    Enter a filename: dataC.txt
    [10,19]: **************************
    [20,29]: **********
    [30,39]: *************************
    [40,49]: ***************************************
    
    Note: You can freely use either a while-loop or for-loop.