1
h02
CS16 s20
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
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: Chapter 2: Variables and assignments, Input/output, Data types and expressions, Simple flow control

ready? assigned due points
true Fri 04/03 11:00AM Fri 04/10 11:59PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GAUCHOSPACE. There is NO MAKEUP for missed assignments;


Please:

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

Complete your reading of Chapter 1, section 1.3 pages 27-32, Chapter 2 sections 2.1 thru 2.4. You don’t need to turn this homework in. To earn credit for this homework, complete the corresponding quiz on gauchospace AFTER you have completed the pen and pencil version of the homework. The quiz will be available at least one day before the due date indicated on the homework.

    1. (6 pts) The author describes the difference between "syntax errors" and "logic errors", and also the difference between syntax errors that produce an "error message" vs. those that produce a "warning message". Briefly explain each of the items below in a way that makes the DIFFERENCES among them clear:
    i. Syntax errors that result in an error message:
    ii. Syntax errors that result in an warning message:
    iii. Logic errors:
    2. (1 pt) If the following statement were in a C++ program, what would it do?
    cout >> "I love oranges and apples";
    3. (1 pt) If the following statement were in a C++ program, what would it do?
    cout << "The world goes round and round"
    4. (4 pts) Show 2 different ways to initialize variables in C++?
    5. (2 pts) Is this variable declaration statement in C++ a good one? Why or why not?

    double int=30;

    6. (2 pts) How do you write the following in ONE LINE in C++: Add a to b and subtract that sum from c, then divide that result by d, and store the final result in e? Assume all variables are of type double.
    7. (4 pts) Explain via an example what a "type mismatch" is. Also explain how compilers handle C++ statements that have a type mismatch?
    8. (4 pts) What does the keyword 'auto' do for the programmer in the C++11 declaration:

    auto x = 2.73;

    9. (4 pts) Write an if-else statement that outputs the string "Grade is B" if the variable `score` is between 80 and 90 (both limits included). Otherwise the if-else statement should output "Grade is not B" ?
    10. (4 pts) The program below intends to do the following: Repeatedly prompt the user to input an integer number. When the user no longer wants to continue entering numbers, output the sum of all the positive numbers entered by the user followed by the sum of all the negative numbers entered by the user. However, the given program has errors. Mark all logical and syntax errors in the program and provide corrections in the space provided to the right. Add missing statements if any.
    #include <iostream>
    
    int main(){
    
        int a , sumPositive, sumNegative;
    
        string promptContinue ="\nTo continue enter Y/y\n";
    
        string promptNum = "\nEnter a number: ";
    
        char response;
    
        while (response = 'y'||`Y') {
    
           cout << promptNum;
    
           cin >> a;
    
           if(a)
              sumPositive+=a;
    
           else
              sumNegative+=a;
    
           cout<< promptContinue;
    
    
    
        }
    
        cout<< "Sum of all the positive numbers is: "<< sumPositive<<endl;
    
        cout<< "Sum of all the negative numbers is: "<< sumNegative<<endl;
    
        return 0;
    }
    
    11. (4 pts) What is the output of the following C++ statements? EXPLAIN WHY.
    int x=20, y=5;
    
    bool v, w;
    
    v = (x != y);
    
    w = ((x/=y) == 4);
    
    cout << x << " " <<y << " "<< v << " "<< w << endl;