1
h03
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")

h03: Homework 3: Basic C++

ready? assigned due points
true Tue 04/11 12:00PM Thu 04/13 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.
In place of that, we drop the two lowest scores (if you have zeros, those are the two lowest scores.)


Please:

  • No Staples.
  • No Paperclips.
  • No folded down corners.

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 - DO NOT TURN IN ANY EXTRA PAPERS!

    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. (2 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. (12 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 4 decimal places.
    6. (4 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. (4 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. (12 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!