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

h05: Homework 5: Advanced Flow Control

ready? assigned due points
true Tue 04/18 12:30PM Thu 04/20 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.)


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

    1. (1 pts) What is a run-time error?
    2. (2 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" customer. 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. (7 pts) What will this piece of code print out exactly?
    #include <iostream>
    using namespace std;
    int main () {
       int x(12);
       while (x >= 1) { 
    	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;
    
    4. (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.