1
h04
CS16 S17
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")

h04: Homework 4: Flow Control and Loops in C++

ready? assigned due points
true Thu 04/13 12:30PM Tue 04/18 12:30PM

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. There is NO MAKEUP for missed assignments.
In place of that, we drop the two lowest scores (if you have zeros, those are the two lowest scores.)


Read Chapter 2 and Ch 3 (If you do not have a copy of the textbook yet, there is one on reserve at the library under “COMP000-STAFF - Permanent Reserve”).

PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL!

    1. (1 pts) Which Boolean operator, when used with 2 logic expressions, yields TRUE if either expression is TRUE?
    2. (1 pts) What is wrong (if anything) with this C++ expression?
    if (size = 9)
       cout << "The size is too small";
    
    3. (2 pts) Show the output of this code if x is of type int.
    int x = 20;
    while ( x > 0) {
       cout << x << endl;
       x = x – 4;
     }
    
    4. (2 pts) Write a while loop that prints out these 11 lines:
    COUNTDOWN TO ZERO: 10
    COUNTDOWN TO ZERO: 9
    ...
    COUNTDOWN TO ZERO: 1
    COUNTDOWN TO ZERO: LIFT OFF!
    
    5. (6 pts) What is the result (i.e. TRUE or FALSE) of the following Boolean operations in C++, given the following: x = 7, y = 10, z = 12
    a) (x == 6)
    
    
    b) ((x == 6) || (y < 20))
    
    
    d) ((!(x < z) || (y > z)) && (z == 12) && (y == 10))
    
    .	
    
    7. (6 pts) Use precedence rules to re-write the following expressions:
    a) x / 4 - 3 == 1 || 62 * y > 0
    b) m / n * n < 10 || --flag <= 0
    c) z / m + n / v == 5 && b * c - d != 0 || g != f
    8. (4 pts) What is the output of the following statements?
    int s = 1;
    do
         cout << s << " ";
    while (s++ <= 5);
    
    9. (4 pts) Same question as above, but the last statement now reads:
    while (++s <= 5);
    
    10. (6 pts) Write a block of statements (like a function, but you don't have to write it like a function) that take three arguments of user inputs of type int and returns a Boolean TRUE if the arguments are in ascending order; otherwise, it returns false. You can use any type of C++ statement types that we've gone over in class so far to accomplish this goal. For example, the set of (1, 2, 3) returns TRUE, as does (5, 5, 8), but not (6, 6, 6), nor (10, 11, 4), etc...
    11. (8 pts) Write a block of statements (like a function, but you don't have to write it like a function) that will repeatedly ask the user to input a number, then print that number multiplied by 3. This repetition only stops when the user enters zero or any negative number as input.