CS 302 Quiz 2

CS 302 - Algebraic Language Programming Name: Colby O'Donnell
Section 22
Instructor: Colby O'Donnell Score: 20 / 20 Points
Date: Wednesday, October 7, 1997 Percentage of Class Grade: 1%

  1. (____ / 4 points) Fill in the blank

      a) The integer zero has a boolean value of false. The boolean value true is represented by the integer(s) non-zero integers.

      b) What is the boolean value of this English expression: "I got up on the right side of my bed and pigs can fly." false. Hint: Assume that we don't throw the pig in the air, and that it doesn't jump off a cliff. (ie. pigs really can't fly)

      c) The while construct always executes its action at least once. True or False: false.

      d) After executing the following code, the value of p will be 2, because p=3 evaluates to true.

               int p = 4;
               if (p = 3) p = 2;
      
  2. (____ / 8 points) Short Answer

    a) Fill in the truth table: (You can abreviate true as T and false as F).

    a b a && b a || b !a
    false false FFT
    false true FTT
    true false FTF
    true true TTF

    b) What is wrong with the following code fragment?

    At the second line, n is not initialized, so the value of n%2 is unpredictable.
         int n;
         while (n % 2) {
           cout << "Please enter an even number: ";
           cin >> n;
         }
         cout << "Thank you." << endl;
         
    c) List four relational operators, and state what type they evaluate to:
         ==  !=  <=  >=
         &&  ||  <   >
    
         They ALL evaluate to the bool type.
    
    d) Explain what the variable 's' represents when it is printed to the screen.

    Variable "s" represents the maximum integer entered by the user.
        cout << "Please enter a list of integers terminated by a zero:";
        int s = 0;
        int n;
        do {
          cin >> n;
          if (n > s) s = n;
        } while (n);
        cout << s;
        
  3. (____ / 3 points) Give the values of a, b, and c after the execution of the following code:

    a: 0, 4, 9, 18
    b: 0, 4, 5, 9
    c: 0, 1, 2,

    Final values: a == 18, b == 9, c == 2

        int a, b = 0, c;
        for (a = 0 ; a <= 10 ; a += b) {
          c = b % 3;
          switch (c) {
            case 0:    b = a+4;   break;
            case 1:    b = a+1;   break;
            default:   b = a;     break;
          }
        }
    
  4. (____ / 5 points) Write a program that asks the user for a positive, odd number, and then prints all the odd numbers from 1 to that number.
    • The program should keep asking for a valid number until the user enters one.
    • To save time, don't bother commenting the code. However, you should still indent properly and use descriptive variable names.
    include <iostream.h>
    void main () {
      int n;
      bool valid;
    
      do {
        cout << "Please enter a positive, odd number: ";
        cin >> n;
        valid = (n > 0 && n % 2);
        if (!valid) cout << "That's not a valid number" << endl;
      } while (!valid);
    
      cout << "Odd numbers from 1 to " << n << " are:" << end;
      for (int i = 0 ; i <= n ; i += 2) cout << i;
    }