L-33 MCS 260 Mon 5 Nov 2001

Review Questions on Chapters 5 to 8

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 be typical for the exam, but to test you current knowledge of the materials.

  1. Give a definition of the following function
             int hash ( char c );
             /* returns 0 for c equal to a,b,c,..,or g,
                        1                h,i,j,..,or n,
                        2                o,p,q,..,or t,
                        3                u,v,w,..,or z   */
    

  2. Write a graceful print that avoids control characters, using the prototype
            void graceful_print ( int c );
            /* prints c if it is not a control character,
               otherwise nothing happens */
    

  3. Write a program that reads in a sequence of characters and that prints out the same sequence, except with all newline symbols replaced by tabs.

  4. Give a definition of the following function
            int bitcnt ( int n );
            /* returns the number of ones in the binary representation
               of the integer number n */
    

  5. Write a program that reads two integers, makes their quotient in double floating-point arithmetic and prints this quotient with 15 decimal places.

  6. Define an enumeration type speed that can take the following values :
           { walking, slow, normal, fast, racing }
    

    1. Adjust your definition of speed so that the five values correspond to the respective integers (expressing mph) : 5, 20, 45, 60, 90.

    2. Write input/output routines for the type speed.
      The input routine scans the corresponding integer value defined above. Use call-by-reference.
      The output routine writes a meaningful string for each speed level.

    3. Give a definition of the following function
        float distance ( speed s, float t );
        /* returns miles traveled when driven at the given speed s for t hours */
      

  7. Consider the following program
         #include<stdio.h>
         int main(void)
         {
            float e = 2.718;
            float *p = &e;
    
            printf("%f", *p);
            *p = 2.718281;
            printf("%f", *p);
    
            return 0;
         }
    
    Draw a diagram to illustrate the relations between p and e. Indicate the values of those variables before and after executing the last assignment. What does the program print?

  8. Consider the following program
         int OddAdd ( int *s, int n );
    
         int main(void)
         {
            int sum = 0;
    
            OddAdd(&sum,3);
     
            return 0;
         }
    
         int OddAdd ( int *s, int n )
         {
            int a = (n % 2 == 1)? n : 0;
    
            *s += a;
         }
    
    Make a diagram representing all variables in main and OddAdd. Illustrate with the diagram what happens before, during, and after the call of OddAdd(&sum).

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.