L-15 MCS 260 Mon 24 Sep 2001

Review Questions on Chapters 1 to 4

The quizzes give you a good idea of the style of questions. But unlike the quizzes, the exam is closed book.

Following are questions to stimulate the preparation of the exam. The questions below are not meant to by typical for the exam, but to test you current knowledge of the materials.

  1. Consider the statements below. What is printed?
           int n = 13;
           printf("%x", n);
    

  2. What does this program print?
         #include <stdio.h>
    
         int main(void)
         {
           int i = 4;
           int j = 5;
    
           while (i--) if (i % 2)
           while (j--) printf("%d\n",j);
           else j = i; return 0;
         }
    
    Make a table with three columns: step number in while loop, value for i and value for j.

  3. For some int n, write a loop to compute the sum of the first n numbers. Write this loop, once using a while, once with a do, and finally once with a for. What kind of loop is most appropriate for this exercise? Explain why.

  4. Explain the difference between break and continue Give an example of both.

  5. Give the definition of a function to compute the step function
             f(x) = 0 if x < 0,
                  = 1 if x >= 0 and x < 1,
                  = 2 if x >= 1 and x < 2,
                  = 3 otherwise
    
    where x and f(x) are of type float. Can you use a switch? Explain why you can or cannot.

  6. Write a basic calculator. The calculator can add, subtract, multiply, and divide two floats at once. The program first reads the two arguments and then the symbol for the operation (+,-,*,\). Before ending, the program prints the result of the calculation. With calc as name of the program, here is a session :
         [jan@galois]% calc
         8.2
         3
         *
         24.6
         [jan@galois]% 
    
    Give first a basic version without any control on input or printing of error messages. Then identify what can go wrong and make the program more robust adding extra checks and more user friendly by printing error messages.

  7. For the roulette game of project two, suppose you had to use the function with prototype :
         int reward ( int outcome, int bet )
         /* returns the tokens gained depending on the values of bet 
            and the outcome; returns 0 if the token used to place the
            bet can be kept, and -1 in all other cases */
    
    Give the main program to implement the project.

Please note the policy on skipping exams:

If an exam is missed, then greater weight will be placed on the final exam, especially on the material covered on the missing exam.

What this means is that if you decide not to take one midterm exam, your final exam will be weighted for one hundred points more.

What it does NOT mean is that you can drop the score of a midterm exam. If you take the midterm, then your score counts. So, please be prepared when you show up for the exam.