1
h02
CS16 M18
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h02: Variables and assignments, input/output, data types and expressions, simple flow control

ready? assigned due points
true Tue 07/03 08:00AM Tue 07/10 11:00AM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the four lowest scores (if you have zeros, those are the four lowest scores).


Reading: Section 1.3, Sections 2.1 - 2.4

  1. (10 pts) Fill in the information in the header. The following are required to get the 10 "participation" points.
    • Filling in your name and umail address.
    • Also: For paper submission PLEASE submit on ONE SHEET OF PAPER, double-sided if at all possible. If you must submit on two printed sheets write name on BOTH sheets and no staples, paperclips, or folded corners.
  2. (9 pts) The author describes the difference between "syntax errors" and "logic errors", and also the difference between syntax errors that produce an "error message" vs. those that produce a "warning message". Briefly explain each of the items below in a way that makes the DIFFERENCES among them clear:
    i. Syntax errors that result in an error message:
    ii. Syntax errors that result in an warning message:
    iii. Logic errors:
  3. (2 pts) If the following statement were in a C++ program, what would it do? Write the correct statement if there is a syntax error.
    cout >> "I love oranges and apples";
  4. (2 pts) If the following statement were in a C++ program, what would it do? Write the correct statement if there is a syntax error.
    cout << "The world goes round and round"
  5. (4 pts) Show 2 different ways to initialize variables in C++?
  6. (4 pts) Is this variable declaration statement in C++ a good one? Why or why not?

    double int = 30;

  7. (4 pts) How do you write the following in ONE LINE in C++: Subtract a from b and add that sum to c, then divide that result by d, and store the final result in e? Assume all variables are of type double.
  8. (5 pts) Explain via an example what a "type mismatch" is. Also explain how compilers handle C++ statements that have a type mismatch?
  9. (6 pts) Write an if-else statement that outputs the string "Grade is B" if the variable score is between 80 and 90 inclusively. Otherwise the if-else statement should output "Grade is not B"?
  10. (4 pts) What is the output of the following C++ statements? Explain why.
    int x = 20, y = 5;
    
    bool v, w;
    
    v = (x != y);
    
    w = ((x /= y) == 4);
    
    cout << x << " " << y << " " << v << " " << w << endl;