L-41 MCS 260 Mon 26 Nov 2001

Answers to Review Questions I for Final Exam

The questions below are meant to stimulate the preparation of the final exam. They cover some of the most important but not all topics in the course. You must review all course materials: textbook, exercises, lecture notes, quizzes, and exams.

  1. Consider the statements below. What is printed?
             int n = 11;
             printf("%o", n);
    
    Answer: %o stands for octal; 13 is printed.

  2. Consider the following code:
           int i, s = 0;
           do
           {
              scanf("%d", &i);
              s += i;
           } while (i != 0);
           printf("%d", s);
    
    1. Write an equivalent piece of code using for instead of do-while.

      Answer:

            int i, s = 0;
            for (;;)
            {
               scanf("%d", &i);
               s += i;
               if (i == 0) break;
            }
            printf("%d", s);
      
    2. Write an equivalent piece of code using while instead of do-while.

      Answer:

            int s = 0;
            int i = 1;
            while (i != 0)
            {
               scanf("%d", &i);
               s += i;
            }
            printf("%d", s);
      
    3. Suppose the user types 1 3 -4 8 5 0. Execute the code and make a table with three columns: step number in the loop, value for i and value for s. What is printed?

      Answer:

            +--------+-------+-------+
            |  step  |   i   |   s   |
            +========================+
            |    0   |  ---  |   0   |
            |    1   |   1   |   1   |
            |    2   |   3   |   4   |
            |    3   |  -4   |   0   |
            |    4   |   8   |   8   |
            |    5   |   5   |  13   |
            |    6   |   0   |  13   |
            +------------------------+
      
      So, 13 is printed.

  3. Consider the following code:
           int n, i = 0;
           int ws = 0;
           while (i++ < 5)
           {
              scanf("%d", &n);
              ws += n/i;
           }
           printf("%d", ws);
    
    1. Write an equivalent piece of code using for instead of while.

      Answer:

             int n, i, ws = 0;
             for (i = 1; i <= 5; i++)
             {
                scanf("%d", &n);
                ws += n/i;
             }
             printf("%d", ws);
      
    2. Write an equivalent piece of code using do-while instead of while.

      Answer:

             int n, i = 1;
             int ws = 0;
             do
             {
                scanf("%d", &n);
                ws += n/i;
                i++;
      
             } while (i <= 5);
      
             printf("%d", ws);
      
    3. Suppose the user types 1 3 -4 8 5. Execute the code and make a table with four columns: step number in the loop, value for i, value for n, and value for ws. What is printed?

      Answer:

            +--------+-------+-------+--------+
            |  step  |   i   |   n   |   ws   |
            +=================================+
            |    0   |   0   |  ---  |    0   |
            |    1   |   1   |   1   |    1   |
            |    2   |   2   |   3   |    2   |
            |    3   |   3   |  -4   |    1   |
            |    4   |   4   |   8   |    3   |
            |    5   |   5   |   5   |    4   |
            +---------------------------------+
      
      So, 4 is printed.

  4. Consider the following code:
           int max, n, i = 0;
           scanf("%d",&max);
           for (i = 1; i < 5 ;i++)
           {
              scanf("%d", &n);
              if (n > max) max = n;
           }
           printf("%d", max);
    
    1. Write an equivalent piece of code using while instead of for.

      Answer:

             int max, n, i = 1;
             scanf("%d", &max);
             while (i < 5)
             {
                scanf("%d", &n);
                if (n > max) max = n;
                i++;
             }
             printf("%d", max);
      
    2. Write an equivalent piece of code using do-while instead of for.

      Answer:

             int max, n, i = 1;
             scanf("%d", &max);
             do
             {
                scanf("%d", &n);
                if (n > max) max = n;
                i++;
      
             } while (i < 5);
             printf("%d", max);
      
    3. Suppose the user types 1 3 -4 8 5. Execute the code and make a table with four columns: step number in the loop, value for i, value for n, and value for max. What is printed?

      Answer:

            +--------+-------+-------+---------+
            |  step  |   i   |   n   |   max   |
            +==================================+
            |    0   |   0   |  ---  |    1   |
            |    1   |   1   |   1   |    1   |
            |    2   |   2   |   3   |    3   |
            |    3   |   3   |  -4   |    3   |
            |    4   |   4   |   8   |    8   |
            |    5   |   5   |   5   |    8   |
            +---------------------------------+
      
      So, 8 is printed.

  5. Write a function int ask_user() that after the display of the menu
       Choose one of the following options :
         D. make a deposit;
         W. withdraw funds;
         A. ask customer assistance;
         R. return to the main menu;
         T. terminate the session;
       Type D, W, A, R, or T to choose : 
    
    reads the character typed in by the user and returns 0,1,2,3, or 4, corresponding to the 5 possible character choices. An error message should be displayed and -1 must be returned in case the user makes an invalid choice.

    Answer:

          int ask_user()
          {
             int choice;
    
             printf("Choose one of the following options :\n");
             printf("  D. make a deposit;\n");
             printf("  W. withdraw funds;\n");
             printf("  A. ask customer assistance;\n");
             printf("  R. return to the main menu;\n");
             printf("  T. terminate the session;\n");
             printf("Type D, W, A, R, or T to choose :\n");
      
             choice = getchar();
    
             switch(choice)
             {
                case 'D' : return 0;
                case 'W' : return 1;
                case 'A' : return 2;
                case 'R' : return 3;
                case 'T' : return 4;
                default : printf("Invalid choice\n");
                          return -1;
             }
          }
    

  6. The "pental" number systems uses digits 0,1,2,3,4. For example, 123 in the pental system evaluates to the decimal system into 1 x 5^2 + 2 x 5^1 + 3 x 5^0.
    1. Convert 68 from decimal to the pental number system.

      Answer:
      68/5 = 13, 68 % 5 = 3
      13/5 = 2, 13 % 5 = 3
      2/5 = 0, 2 % 5 = 2
      Thus, 68 is written like 233 in pental notation.
      Check : 2*5^2 + 3*5^1 + 3*5^0 = 50 + 15 + 3 = 68.

    2. Consider the following functions
         void read_forwards ( int *n );
         /* reads a positive number in pental notation,
            starting with the most significant digit */
         void read_backwards ( int *n );
         /* reads a positive number in pental notation,
            starting with the least significant digit */
         void write_forwards ( int n );
         /* writes a positive number in pental notation,
            starting with the most significant digit */
         void write_backwards ( int n );
         /* writes a positive number in pental notation,
            starting with the least significant digit */
      
      Which of these functions need arrays to define? Give the definitions of the functions.

      Answer:

      /* L-41 MCS 260 Mon 26 Nov 2001 : solution to question 6.
      
      The program below is an interactive testing facility for the
      reading and writing of positive integers, forwards and backwards. */
      
      #include<stdio.h>
      
      void read_forwards ( int *n );
      /* reads a positive number in pental notation,
         starting with the most significant digit */
      void read_backwards ( int *n );
      /* reads a positive number in pental notation,
         starting with the least significant digit */
      void write_forwards ( int n );
      /* writes a positive number in pental notation,
         starting with the most significant digit */
      void write_backwards ( int n );
      /* writes a positive number in pental notation,
         starting with the least significant digit */
      
      int main(void)
      {
         int n;
      
         printf("Give a positive integer : "); 
         scanf("%d", &n);
      
         printf("  %d in pental notation, forwards  : ", n);
         write_forwards(n);
         printf("\n");
      
         printf("  %d in pental notation, backwards : ", n);
         write_backwards(n);
         printf("\n");
      
         n = getchar();  /* skip newline symbol */
      
         printf("Give pental number forwards : ");
         read_forwards(&n);
      
         printf("  %d in pental notation, forwards  : ", n);
         write_forwards(n);
         printf("\n");
      
         printf("Give pental number backwards : ");
         read_backwards(&n);
      
         printf("  %d in pental notation, backwards : ", n);
         write_backwards(n);
         printf("\n");
      
         return 0;
      }
      
      void read_forwards ( int *n )
      {
         int d;
      
         *n = 0;
         for (;;)
         {
            d = getchar();
            if ((d >= '0') && (d <= '4'))
               *n = *n * 5 + (d - '0');
            else
               break;
         }
      }
      
      void read_backwards ( int *n )
      {
         int d;
         int pow5 = 1;
      
         *n = 0;
         for (;;)
         {
            d = getchar();
            if ((d >= '0') && (d <= '4'))
               *n = *n + (d - '0') * pow5;
            else
               break;
            pow5 *= 5;
         }
      }
      
      void write_forwards ( int n )
      {
         int digits[32];
         int i,wn;
      
         for (wn = n, i = 0; wn > 0; wn /= 5, i++)
            digits[i] = wn % 5;
      
         while (i-- > 0)
            printf("%d", digits[i]);
      }
      
      void write_backwards ( int n )
      {
         int wn;
      
         for (wn = n; wn > 0; wn /= 5)
            printf("%d", wn % 5);
      }
      

FINAL EXAM is in Lecture Center C6 on Monday 3 December 2001 from 1:00 till 3:00PM.

Please let me know as soon as possible if you cannot attend the exam and need to schedule a makeup.

Incomplete grades are only given in exceptional cases. At least the two following conditions must be satisfied: the student is in good standing (has decent scores on midterm exams, projects, and quizzes) and the student is unable to take an exam during the finals week.