Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

See the bottom for how to submit your work.

  1. [10 pts] We would like you to self-test how much you retained from the lecture. Write the correct solution to the Mandatory Practice Problems. Debug your code until it runs correctly. Circle below how you wrote your code.
    1. (10 pts) I was able to write the code without referring to anything.
    2. (10 pts) I had to look at the notes, but still I was able to write the code without looking at the solution.
    3. (10 pts) I had to look at solutions to finish to code.
    4. (0 pts) I didn't do this.
    If you had to look at the solution, briefly describe what you missed but understand now.
    
    
    
    
  2. [10pts] Rewrite the following code so that the for-loops are replaced with while-loop loops. Use the empty space below.
    
    // 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
    
  3. [10pts] We will write a program that prints out the following:
    A
    AB
    ABC
    ABCD
    ABCDE
    

    Fill out the the source code on the right.

    Warning: Only fill in the blanks. Do not add other code elsewhere.

    
      int i=1;
      char c;
    
      while(_____________)
      {
        for(int j=0; ____________ ; j++ )
        {
          c = ______________;
          cout << c;
        }
    
        cout << endl;
        i++;
      }
    
  4. [10pts] Consider the following source code:
    
      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;
      }
    
    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.
    
    
    
    
    
    
  5. [60pts] Write a program with a source file name hw.cpp that reads in names and birthdates from a given file and reports the number of adults and children in the list.

    Your program should start by asking for a filename from the command line. If the file does not exist, print an error message and exit.

    Each line in the file will have a single name and birthday. Each name is a single string, and each birthday is formatted as MM/DD/YY, so for example the birthday 08/04/32 means August 4, 1932. You may assume that each birthday is between 1920 and 2019.

    After reading in the file, your program should report the number of adults and children in the file. For the purposes of this problem, an adult is anyone born on or before 09/27/00. Anyone born after that date is considered a child.

    On ther right is a listing of names1.txt, along with an indication of who is an adult or a child:

    John 09/04/01
    Dennis 09/09/00
    Margaret 08/17/00
    Ken 12/17/20
    Barbara 11/07/00
    Linus 09/28/00
    Grace 12/09/06
    Frances 08/04/32
    
    child
    adult
    adult
    adult
    child
    child
    child
    adult
    

    We provide two sample files for testing: names1.txt and names2.txt. Your program must work for any file that is correctly formatted, not just these two.

    Here are some sample runs (user input in red):

    ~/$ ./hw
    Filename: names1.txt
    4 adults and 4 children
    
    ~/$ ./hw
    Filename: names2.txt
    10 adults and 9 children
    
    ~/$ ./hw
    Filename: badname.txt
    File not found!
    
    Tip: You don't need to use a for loop for this assignment. Rather, using a while loop is recommended.

Turn in