Name: ____________________________________________________ Alpha: ________________________

Describe help received: _____________________________________________________________________

  1. Read the sections of Visual Layout, Comments, and Naming in the programming style guide and tips.
    Warning!

    Points will be deducted if you violate the the programming style guide!

    • Your code should have good indentations and line breaks.
    • Give appropriate comments to your code.
    • Your variable names should be chosen well.
  2. (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 "Roots of Quadratic Polynomials" -- 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.
    
    
    
    
  3. (70pts) Consider the following diagram with a large right triangle containing a small right triangle:

    The length of AC is \(\sqrt{x^2-y^2}\). The length of BD is \(\sqrt{|DC|^2+y^2}\), where \(|DC|\) is the length of DC.

    You are to write a program that works as follows:

    • The program gets lengths x, y and z from the user
    • Then, it computes the length of the dashed line segment BD.
    • Your file must be named hw.cpp. You may assume that the user inputs valid data.
    • Your output must be in exactly the form shown in the examples below.

    The red text in the below examples is user input.

    Example 1

    $ ./a.out
    Enter length x: 3.5
    Enter length y (<3.5): 1.8
    Enter length z (<3.00167): 2.1
    Length of BD is 2.01321
    

    Example 2

    $ ./a.out
    Enter length x: 5.1
    Enter length y (<5.1): 4.8
    Enter length z (<1.72337): 1.5
    Length of BD is 4.80519
    

    Example 3

    $ ./a.out
    Enter length x: 3.5
    Enter length y (<3.5): 1.8
    Enter length z (<3.00167): 0.25
    Length of BD is 3.28811
    

    To explain the conditions for y and z in the above, because we have a triangle,

    Hint: A great way to write a program is to get a little bit working, compile and debug, then get a little more work, compile and debug, etc. So you might start trying to get something like this working:

    $ ./a.out
    Enter length x: 5.1
    Enter length y (<5.1):
    

    Once you've tested and debugged that, try and extend it so that the following works:

    $ ./a.out
    Enter length x: 5.1
    Enter length y (<5.1): 4.8
    Enter length z (<1.72337):
    

    Once that works, you can try to finish the whole thing off.

  4. (4 pts) What's the output of the following code snippet?
    
    cout << 3/4 << endl;
    cout << 20 % 7 << endl;
    
  5. (10 pts) What is the type of each of the following values?
     -0      0.0     '0'    "-0.0"       false 
  6. (8 pts) Fill out the table
    CodeUser InputProgram OutputUnread Input
    char c;
    cin >> c;
    cout << c << endl;
    103.07% 1.2%
    int k;
    cin >> k;
    cout << k << endl;
    103.07% 1.2%
    double x;
    cin >> x;
    cout << x << endl;
    103.07% 1.2%
    string s;
    cin >> s;
    cout << s << endl;
    103.07% 1.2%

Turn in