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

h03: Chapter 3: Boolean expressions, multiway branches, more loops

ready? assigned due points
true Fri 04/10 11:00AM Fri 04/17 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.

Read Ch 3.1 -3.3, pages 112-118,120 -154. 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.

PLEASE WRITE ALL YOUR ANSWERS IN THIS SHEET. HOMEWORKS SUBMITTED IN A FORMAT DIFFERENT FROM THE PROVIDED TEMPLATE WILL RECEIVE 0 POINTS. PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL!

    1. (6 pts) Precedence rules determine how the compiler groups operators and operands when evaluating an expression in the absence of paranthesis. For each of the following expressions add paranthesis to show how the expression is evaluated according to the C++ precedence rules?
      a) x == 2 || y > 20
    
      b) !y && x < 9 * 2 || y < x
    
      c) x + 7 > 10 || x + 23 < -7
    
    2. (2 pts) Show the output of the following code AND also write the final value of x after the code is executed.
    int x = 17;
    while ( x > 0) {
       cout << x << endl;
       x = x – 5;
     }
    
    3. (2 pts) What is the outcome of the following code?
    int x = 0;
    while ( x = 2 && x <10) {
       cout << x << endl;
       x+=2;
     }
    
    5. (6 pts) What is the result (i.e. TRUE or FALSE) of the following Boolean expressions in C++, given that: x = 2, y = -1, z = 0 in each case
      a) (x == 6)
      b) !(y > 0)
      c) ((x == 2) || (y > 20))
      d) ((x >= 3) && (z <= 12))
      e) ((x > y) && (y < z))
      f) ((!(x < z) || (y > z)) && (z == 12) && (y == 10))
    
    
    6. (9 pts) Using plain and/or multiway if-else statements ONLY write a program that outputs the grade of a student given an integer variable 'score'. The program should read the student's score from standard input. If the score is outside the range of 0 and 100, the program should output "Not a valid score" and exit. Otherwise it should output the string "Grade is X" where X is replaced by the student's letter grade according to the following rubric:
    90 <= score <=100, grade = 'A'| 80 <= score < 90 , grade = 'B'| 70 <= score < 80 , grade = 'C'|
    60 <= score < 70 , grade = 'D' | 0 <= score < 60 , grade = 'F'
    

    …..a) Make your program easy to read and understand. Format your answers so that they are readable on the display. This means consider your use of “\n” characters.

    …..b) Test this program out by compiling it and running it the same way you do with lab assignments. When you are sure that you have a working program, write it out in the space provided below. You will be graded for programming style as well as having a functionally correct program.

    7. (2 pts) What is a runtime error in C++?
    8. (6 pts) Use precedence rules to re-write the following expressions:
    (i) x / 4 - 3 == 1 && 6 * x + y > 0
    (ii) a / b * b < 0.5 || --flag >= 0
    (iii) z * m + n / v >= 5 && b + c - d != 0 || f < g
    9. (4 pts) What is the output of the following statements?
    int p = 5;
    while (--p > 0)
         cout << p << " ";
    
    10. (4 pts) What is the output of the following statements?
    int s = 1;
    do
         cout << s << " ";
    while (s++ <= 5);
    
    11. (4 pts) Same question as above, but the last statement now reads:
    while (++s <= 5);
    
    12. (10 pts) Write a function named 'check_descending' that takes three arguments of type int and returns true if the arguments are in descending order; otherwise, it returns false. You can use any type of C++ statements that we've gone over in class so far to accomplish this goal.
    13. (6 pts) The infinite series: s = 1 + (2/3) + (4/9) + (8/27) + .... is a geometric series that converges to a whole rational number (i.e. like 2 or 3 or 4). Below is an unfinished C++ program that will calculate s to the kth position (so, for example, if k = 1, then s = 1 + (2/3) = 1.666...). Fill in the missing code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main(){
        double s(0.0);
        int k(0);
        cout << "Enter k: ";
        cin >> k;
        for (____________________________________________________) {
    
            s = ____________________________________________________; }
    
        cout << "Series converges to: " << s << endl;
        return 0;
    }
    
    14. (8 pts) Compile and run the above completed program. What is the value of s if k = 6? if k = 19 ? What whole rational number does s look like it's approaching? What is the smallest value of k for s to be shown by the program as exactly equal to that whole number?