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 Practice Problem of
     Checking if a given input is a letter or not
    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] Run the following code a few times. It is supposed to output Good input if the user enters a positive integer that divides 1155 evenly, and Bad input for anything else.

    
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int k;
      cout << "Enter positive integer that divides 1155: ";
      cin >> k;
      if (1155 % k == 0 && k > 0)
        cout << "Good input" << endl;
      else
        cout << "Bad input" << endl;
      return 0;
    }
    
    1. [4pts] Find an input that makes the program crash.
      Input: 
    2. [6pts] Explain why the program crashes, and briefly describe how it could be fixed.
    3. 
      
      
      
      
      
  3. [20pts] Fill in the table on the right.
    int n = 5;
    double x = 10.5;
    char c = 'R';
    string s = 'Navy';
    string t = 'Army';
    
    Note: each expression should be taken as independent. I.e. if one expression modifies some variable values, those modifications do not carry over to the next expression.
    expressiontypevalue
    n = 2
    n == 2
    c < 200
    1 != x
    1 < x < 10
    1 < x && x < 10
    !n
    c == int('R')
    s == t
    s >= t
  4. [5pts] Fill in the conditions on the if statements on the right so that they are equivalent.
    
    if (a <= 'k')
    {
      cout << "easy";
    }
    else
    {
      if (a >= 'q')
        cout << "easy";
      else
        cout << "tough";
    }
    
    
    if (_____________________________________________)
    {
      cout << "easy";
    }
    else
    {
      cout << "tough";
    }    
    
  5. [55pts] Write a program with source file named hw.cpp that works like this (with the user input colored in red):
    ~/$ ./hw
    Values for a and b? 111, 1
    What do you want? add 10 to a
    a = 121 and b = 1
    
    ~/$ ./hw
    Values for a and b? 1.1, -2.2
    What do you want? square b
    a = 1.1 and b = 4.84
    
    That is, the program receives two numbers for a and b (separated by a comma), receives a command, and then executes it.

    The following commands are possible:

    In the above, num can be any real number. Note: assume that the input is always formatted correctly, so you don't need to check whether the input is valid.

Turn in