CS 302 Quiz 5

CS 302 - Algebraic Language Programming Name: Solution
Section 18 E-mail: ________________________@cs.wisc.edu
Instructor: Colby O'Donnell Score: __________________________ / 20 Points
Date: Tuesday, March 17, 1998 Percentage of Class Grade: 2%

  1. Boolean Type (bool)

      a) (____ / 1 point) The integer 0 has a boolean value of false.

      b) (____ / 1 point) The boolean value true is represented by the integer(s) non 0 .

      c) (____ / 2 points) 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

      d) (____ / 2 points) Write the following sentence as a C++ expression without using any function calls: "The absolute value of x+1 is greater than 4."

      x + 1 > 4 || x + 1 < -4 (or) x > 3 || x < -5
  2. (____ / 2 points) The break; and continue; statements only apply to certain C++ constructs. In each box, circle Yes if the statement does apply, and No if it does not.

    C++ Construct break; continue;
    while loop Yes Yes
    for loop Yes Yes
    multiway-if statement No No
    switch statement Yes No

  3. (____ / 3 points) Tracing: Give the values of a, b, and c after the execution of the following code:

    Code Variables
        int a, b = 0, c;
        for (a = 0 ; a <= 10 ; a += b) {
          c = b % 3;
          switch (c) {
            case 0:    b = a+1;   break;
            case 1:    b = a+4;   break;
            default:   b = a;     break;
          }
        }
    
    a: 0 , 1 , 6 , 12

    b: 0 , 1 , 5 , 6

    c: 0 , 1 , 2

  4. (____ / 3 points) Convert the following while loop and multiway-if into a for loop that uses a switch statement. (Hint: you should be able to eliminate the variable j.)

    while and if for and switch
    int i = 2;
    while (i < 8) {
      int j = i % 4;
      if (j == 0 || j == 1) {
        cout << "Monday" << endl;
      } else if (j == 2) {
        cout << "Tuesday" << endl;
      } else {
        cout << "Wednesday" << endl;
      }
      i++;
    }
    
    for (int i = 2 ; i < 8 ; i++) {
      switch (i % 4) {
        case 0:
        case 1:
          cout << "Monday" << endl;
          break;
        case 2:
          cout << "Tuesday" << endl;
          break;
        default:
          cout << "Wednesday" << endl;
          break;  // optional
      }
    }
    

  5. (____ / 6 points) File I/O: The following program tests your knowledge of >>, get(), and getline(). A file called hill.txt will be read a different way each of three times. Each time the file stream is opened with the open() function, the file is read from the the beginning. () is the '\n' character, and () is the end of file. Fill in the program's output in the lower-right box.

    Code File: hill.txt
      ifstream fin;
      int x,y,z;  // Keep track of these variables
      char s[80]; // Basically like declaring: string s;
      char c;
      //-------------------------------------------------
      fin.open ("hill.txt");
      for (x = 0 ; !fin.eof() ; x++) fin.get(c);
      fin.close();
      //-------------------------------------------------
      fin.open ("hill.txt");
      for (y = 0 ; !fin.eof() ; y++)  fin.getline(s, 80);
      fin.close();
      //-------------------------------------------------
      fin.open ("hill.txt");
      for (z = 0 ; !fin.eof() ; z++)  fin >> s;
      fin.close();
      //-------------------------------------------------
      cout << "  x: " << x << endl
           << "  y: " << y << endl
           << "  z: " << z << endl;
    
    Jack and Jill
    went up the hill
    to fetch a pale of water.
    Output
    x: 58

    y: 4

    z: 14