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

h03: Advanced Flow Control and Functions

ready? assigned due points
true Thu 10/12 02:00PM Thu 10/19 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.


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

    1. (3 pts) What is a run-time error?
    2. (3 pts) You are writing a program that keeps track of a bank account for your customers. The program must print out a statement on the computer display that warns the customer that their balance is below $25, but only if they are a "premiere" customers. Which of these choices is correct? Explain WHY?
    A.  if (balance < 25) { cout << "Your balance is under $25 - be careful!"; }
    
    B.  if !((balance > 25) || (type != "premiere")) { cout << "Your balance is under $25 - be careful!"; }
    
    C.  if ((balance < 25) && (type == "premiere")) { cout << "Your balance is under $25 - be careful!"; }
    
    D.  if (!(balance > 25) && (type == "premiere")) { cout << "Your balance is under $25 - be careful!"; }
    
    
    3. (10 pts) What will this piece of code print out exactly?
    #include <iostream>
    using namespace std;
    int main () {
       int x(14);
       while (x >= 3) { 
    	cout << x << " "; 
    	if (!(x % 3)) {
    		cout << "Buzz! ";
    		if ((x % 2) == 0) cout << "Fizz!"; 
    	}
    	else cout << "...";
    	cout << endl;
    	x--;
       }	
       return 0;
    }	
    
    4. (10 pts) Find 5 mistakes in this C++ code, mark them (circle them, put an arrow, etc...), and label each as "logic" or "syntax" error:
    #include <iostream>;
    use namespace std;
    int main () 
    	    int a(0);b(0);
    		cout << "Enter a number that is either 0 or 1: ";
    		cin >> a;
    		cout << "Enter another number that is less than 5: ";
    		cin >> b;
    		switch (a) { 
    			case 0: 
    			cout << "number is zero.\n"; 
    			for (int i = 5; i != b; i++) {
    				cout << "extra line " << i << endl;
    			}
    			break; 
    			case 1: 
    			cout << "number is one.\n"; 
    			if (b > 5) {
    				cout << "Han shot first!\n";
    			}
    			break; 
    		}
    		return;
    
    5. (20 pts) Write a program that first asks for the name of the user, then asks the user to enter an angle between 0 and 180 degrees. If the user does not enter a correct number the program must start over. Once the correct number is entered, then the program must ask the user if it should calculate the sine, the cosine, or the tangent of the angle. If the user does not enter a valid choice, then the program should ask the same question again. Once the choice is made, the program must do the calculation and present the answer to the user.
    You must use a switch statement in this program. You may use built-in functions in the cmath library for the trigonometric functions. Print out this program on a separate page and submit it attached to this homework assignment.
    6. (9 pts) We talked about three concepts that are very important to keep straight, and not confuse: (a) function declaration, (b) function definition, and (c) function call. Here is a short C++ program, with line numbers. Please indicate after the program which line number (or range of line numbers, e.g. 3-5 or 7-14) contains the function prototype, function definition, and function call for the isDivisibleBy function.
    1  #include <iostream>
    2  using namespace std;
    3
    4  bool isDivisibleBy(int a, int b);
    5
    6  int main() {
    7     cout << "result for (15,5) is " << isDivisibleBy(5,15) << endl;
    8     cout << "result for (15,5) is " << isDivisibleBy(5,15) << endl;
    9     return 0;
    10  }
    11
    12  bool isDivisibleBy(int a, int b) {
    13    return ( a % b == 0 );
    14  }
    
    7. (10 pts) Write a function declaration and a function definition for a function that takes one argument of type int and one argument of type double, and that returns a value of type double that is the average of the two arguments.
    8.(10 pts) Which of these uses of type casting will NOT ensure that f is 1.5? Answer should be (ex1), (ex2), (ex3), or (ex4) (or a combination of those).
    int a(1), b(2), c(2), d(2), e(2);
    double f;
    
    f = (a + b)*c / static_cast<double>(d + e);	// (ex1)
    f = static_cast<double>(a + b)*c / (d + e);	// (ex2)
    f = (a + b)*static_cast<double>(c) / (d + e);	// (ex3)
    f = static_cast<double>((a + b)*(c) / (d + e));	// (ex4)
    
    9.(10 pts) Write a void function that takes in 2 arguments (integer n and string s) and produces a print out of the string s n times.
    10.(10 pts) What is the output of the program below (write it in the space to the right)?
    #include <iostream>
    using namespace std;
    
    void times(int y, int &z) {
      y = y * 9;
      z = z + 9;
      cout << "y=" << y << ", z=" << z << endl;
    }
    
    int main() {
      int b = 7, d = 3;
      times(b, d);
      cout << "b=" << b << ", d=" << d << endl;
      return 0;
    }
    
    10.(15 pts) Suppose we have a program where the main starts with the line:
      int main(int argc, char *argv[])  
    
    and the program is run with the following command line:   ~jimbo/cs16/myprog 12 dozen eggs
    Answer the following questions carefully, using the proper quotation marks (where applicable).
    What is the value of argc?
    What is the value of argv[2][2]?
    What is the value of argv[1] and what data type is it?
    What is the value of argv[0]?
    What is the value of argv[1]/4?