1
h02
CS16 F17
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: Basic C++

ready? assigned due points
true Thu 10/05 02:00PM Thu 10/12 02:00PM

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.


Read Chapter 2 (If you do not have a copy of the textbook yet, there is one on reserve at the library under “COMP000-STAFF - Permanent Reserve”). ALL YOUR ANSWERS MUST BE WRITTEN ON THIS PAPER.

    1. (2 pts) Show 2 different ways to initialize variables in C++?
    2. (2 pts) Is this variable declaration statement in C++ a good one? Why or why not?

    double maguro=50;

    3. (2 pts) How do you write in ONE LINE in C++: Add a to b and subtract that sum from c, then divide that result by d?
    4. (4 pts) What will appear on the monitor/display when the following C++ statement is executed?

    cout << "My dog likes to eat\t" << 5 << " cans of dog food a day" << " !" << endl;

    5. (16 pts) Write a short program that asks the user to input three double type inputs from the keyboard, the program then shows the user what the 3 numbers entered were. It then multiplies them together, and displays the answer within 5 decimal places.
    6. (8 pts) Mark up this code to show four things that are missing from this program.
    #include <iostream>
    
    int main()
        int a(0),b(0),c(0);
        string quote;
        cout << "Enter 3 numbers separated by spaces: ";
        cin >> a,b,c;
        sum = a + b + c;
        quote = "The sum of these 3 numbers is: "
        cout << quote << sum;
        return 0; 
    }
    
    7. (6 pts) What is the resulting output display from the following C++ statements? AND EXPLAIN WHY.
    int x(35), y(5);
    bool v, w;
    v = (x >= y);
    w = ((x/y) == 7);
    cout << v << w << endl;
    
    8. (16 pts) Write a C++ program that takes as input from the user, their favorite integer number and their name. The program then, says "Hello, (user's name here), your favorite number is (user's favorite number here)!". Then the program will will multiply the number they gave you by 5, then multiply that result again by 5, then multiply THAT result AGAIN by 5. The program HAS to do so using one of the C++ shorthands we discussed in class.
    Finally, the program should say "I took your number and multiplied it by 5 THREE times and the result was: (the resulting number here)."
    IMPORTANT NOTE: You CANNOT write this program using ANY loop statements!
    9. (2 pts) Which Boolean operator, when used with 2 logic expressions, yields TRUE if either expression is TRUE?
    10. (4 pts) What is wrong (if anything) with this C++ expression?
    if (size = 9)
       cout << "The size is too small";
    
    11. (4 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;
     }
    
    12. (6 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!
    
    13. (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))
    
    .	
    
    14. (4 pts) What is the output of the following statements?
    int s = 1;
    do
         cout << s << " ";
    while (s++ < 5);
    
    15. (4 pts) Same question as above, but the last statement now reads:
    while (++s <= 5);
    
    16. (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 descending 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 FALSE, as does (5, 3, 8), but not (6, 6, 6), nor (10, 6, 4), etc...
    17. (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 23. This repetition only stops when the user enters zero or any negative number as input.