Date: Mon, 11 Nov 1996 17:12:21 GMT Server: NCSA/1.5 Content-type: text/html Last-modified: Sat, 02 Nov 1996 21:55:40 GMT Content-length: 2139 Quiz 3 - CS 302 Fall 1996 - Section 4
Name: _______________________________

CS 302 Fall 1996 - Section 4

Quiz 3

Monday, October 28, 1996


This quiz is 20 total points. Please write legibly. If I can't read it, I can't grade it. You have until 5 minutes after the class period ends to finish. Good luck.
  1. (5 points) Write the struct declaration for a structure called ``Computer'' which has four member variables: an integer disk_size, an integer monitor_size, a character model_type, and a double total_price.







  2. (5 points) Rewrite the following function to use switch statement instead of a nested if-else statement. Your code should have no if statements.
    int foo(int number, int value) 
    {
      if (number == 1) {
        value = value * 2;
      } else if (number == 3) {
        value = value + 10;
      } else if ((number == 4) || (number == 6)) {
        value = value * value;
      } else {
        value = 17;
      }
      return value;
    }
    










    - Over -
  3. (5 points) What does the following code display to the screen?
    #include<iostream.h>
    
    int bar(int number, int value) 
    {
      if ((number == 1) && (value++ > 0)) {
        return value + 10;
      } else {
        return value;
      }
    }
    
    int main() 
    {
      cout << bar(0, 0) << endl;
      cout << bar(1, 0) << endl;
      cout << bar(0, 1) << endl;
      cout << bar(1, 1) << endl;
    }
    




  4. (5 points) Rewrite the following function to use a while or do-while loop instead of a for loop.
    int factorial(int n) 
    {
      int counter, fact = 1;
      for(counter = 1; counter <= n; counter++) {
        fact *= counter;
      }
      return fact;
    }