Midterm 2 with solutions


1. (25 points) Write a program that read 9 integers three by three. For each , the program will print the smallest and the largest number. You have to write three functions:

  1. - which finds the smallest number
  2. - which finds the largest number
  3. - which produces the output
Sample run:
   Enter triplet: 13 43 25
   Min: 13 Max: 43
   Enter triplet: 96 42 1
   Min: 1 Max: 96
   Enter triplet: 11 11 11
   Min: 11 Max: 11


   #include <stdio.h>

   int FindMin(int num1, int num2, int num3);
   int FindMax(int num1, int num2, int num3);
   void Print(int min, int max);
   
   int main()
   {
       int rep = 0, num1 = 0, num2 = 0, num3 = 0;
       for(rep = 0; rep < 3; rep++)
       {
           printf("Enter triplet: ");
           scanf("%d %d %d", &num1, &num2, &num3);
           Print(FindMin(num1, num2, num3), FindMax(num1, num2, num3));
       }
       return 0;
   }
   
   void Print(int min, int max)
   {
       printf("Min: %d Max: %d\n", min, max);
   }
   
   int FindMin(int num1, int num2, int num3)
   {
       int min = num1;
       if( num2 < min )
          min = num2;
       if( num3 < min)
          min = num3;
       return min;
   }
   
   int FindMax(int num1, int num2, int num3)
   {
       int max = num1;
       if( num2 > max )
           max = num2;
       if( num3 > max )
           max = num3;
       return max;
   }


2. (25 points) The number (written as n!) is given by the formula:

n! = 1 * 2 * 3 * ... * (n-1) * n
Write a program that reads the number n from the keyboard and then displays all of the factorials from 1 to n in reverse order. Sample run:
   Enter n: 6
   6! = 720
   5! = 120
   4! = 24
   3! = 6
   2! = 2
   1! = 1
Notes and Hints:


   #include <stdio.h>
   
   int main()
   {
       int number, numLoop, facLoop, fact;
   
       printf("Enter n: ");
       scanf("%d", &number);
   
       for(numLoop = number; numLoop > 0; numLoop--)
       {
           /* Initialize factorial number to the current 'n' */
           
           fact = numLoop;
   
           /* Multiply the current 'n' by all the numbers from 2 to n-1 */
           
           for(facLoop = 2; facLoop < numLoop; facLoop++)
               fact *= facLoop;
   
           printf("%d! = %d\n", numLoop, fact);
       }
   }


3. (20 points) You have a data base where each entry is identified by an integer key (like your social security number, without the dashes) and you are given functions to manipulate the database. In particular, you have the following three functions:

  1. int ItemPosition(int keyValue) - returns the position in the database of the item with key keyValue. If the item is not present, the function returns 0.
  2. void AddItem(int keyValue) - add an item to the data base with key keyValue.
  3. void PrintItem(int keyValue) - print the data of the item with key keyValue.
Write a program that prompts the user for a key, then, using the above functions, searches the data base for the item. If the item exists, print it, else add it to the data base.


   #include 
   
   /* Here goes the header file with the data base functions */
   
   int main()
   {
       int key;
   
       printf("Please enter key: ");
       scanf("%d", &key);
   
       if( ItemPosition(key) != 0 )
       {
           PrintItem(key);
       }
       else
       {
           AddItem(key);
       }
   }


4. (15 points) The following code contains at least 5 errors (syntax and logic). Comments,formatting or lack thereof are not any of the errors. Identify the line number and the error.

    1. #include <stdio.h>

    2. int Height(float surface, int width);

    3. int main()
    4. {
    5.    int width = 0, index = 0;
    6.    float surface = 0;

    7.    for(index = 0; index < 3; index++)
    8.        printf("Enter surface: ");
    9.        scanf("f", &surface);
   10.        printf("Enter width: ");
   11.        scanf("%d", &width);
   12.        printf("Height = %d\n", Height(surface));
    
   13.    return 0;
   14. }

   15. int Height(float surface, int width);
   16. {
   17.    if( width == 0 )
   18.        return (int )(surface/width);
   19. }
  1. Line: 8___    Error: the for loop executes only this line, you need to have brackets before line 8 (and after line 12) to make sense.
  2. Line: 9___    Error: the formatting character "f" should be preceeded by a percentage sign.
  3. Line: 12__    Error: the function Height takes 2 arguments.
  4. Line: 15__    Error: should not have semicolon at the end of the line.
  5. Line: 17__    Error: the conditional statement should read width != 0. This will protect against division by zero.
  6. Line: 19__    Error: missing return value when conditional fails.


5. (15 points) What is the output of the following program ?

   #include <stdio.h>
   
   void Print(int size, int inc);
   int Manip(int value);

   int main()
   {
       Print(8, Manip(8));
       Print(1, Manip(1));
       Print(3, Manip(3));
   
       return 0;
   }
   
   void Print(int size, int inc)
   {
       int index, spaces, reps;
       for(index = 0; index < size; index += inc)
       {
           for(spaces = 0; spaces < index; spaces++)
               printf("_");
           for(reps = 0; reps < inc; reps++)
               printf("%d", inc);
           printf("\n");
       }
   }

   int Manip(int value)
   {
       return (value % 3)+1;
   }


   333
   ___333
   ______333
   22
   1
   _1
   __1