Midterm 1 with solutions


1. (25 points) Write a program that, given a range of temperatures in fahrenheit, will print out a table displaying every fifth fahrenheit temperature and the equivalent centigrade temperature approximated to an integer. The formula to convert from fahrenheit to centigrade is:

c = (f - 32) * 5 / 9
where c is the centigrade temperature and f is the fahrenheit temperature.

Sample run:

   Enter starting temperature: 27
   Enter ending temperature  : 88

   F       C
   27      -2
   32      0
   37      2
   42      5
   47      8
   52      11
   57      13
   62      16
   67      19
   72      22
   77      25
   82      27
   87      30


   #include <stdio.h>
                                                                    
   int main()                                                       
   {                                                                
       int startTemp = 0, endTemp = 0, currTemp = 0;                
                                                                    
       printf("Enter starting temperature: ");                      
       scanf("%d", &startTemp);                                     
       printf("Enter ending temperature  : ");                      
       scanf("%d", &endTemp);                                       
                                                                    
       printf("F\tC\n");                                            
                                                                    
       for(currTemp = startTemp; currTemp <= endTemp; currTemp += 5) 
       {                                                            
           printf("%d\t%d\n", currTemp, (currTemp - 32) * 5 / 9);   
       }                                                            
                                                                    
       return 0;                                                    
   }                                                                


2. (25 points) Write a program that computes the minimum number of coins of different denomination needed to cover an amount expressed in cents. The user will enter the amount and the program will display the minimum number of quarters, dimes, nickels and pennies needed to total the amount (think of it as a program used by a vending machine to compute the coins that it has to return for your change).

Sample run:

   Enter amount (in cents): 117

   4 Quarters
   1 Dimes
   1 Nickels
   2 Pennies
Notes and hints:


   #include <stdio.h>                                                               
                                                                                    
   int main()                                                                  
   {                                                                           
       int quarters = 0, dimes = 0, nickels = 0, pennies = 0, cents = 0;       
                                                                               
       printf("Enter amount in cents: ");                                      
       scanf("%d", ¢s);                                                    
                                                                               
       while( cents >= 25 )                                                    
       {                                                                       
           quarters++;                                                         
           cents -= 25;                                                        
       }                                                                       
                                                                               
       while( cents >= 10 )                                                    
       {                                                                       
           dimes++;                                                            
           cents -= 10;                                                        
       }                                                                       
                                                                               
       while( cents >= 5 )                                                     
       {                                                                       
           nickels++;                                                          
           cents -= 5;                                                         
       }                                                                       
                                                                               
       while( cents >= 1 )                                                     
       {                                                                       
           pennies++;                                                          
           cents -= 1;                                                         
       }                                                                       

       printf("\n");                                                                
       printf("%d\tQuarters\n", quarters);                                          
       printf("%d\tDimes\n", dimes);                                                
       printf("%d\tNickels\n", nickels);                                            
       printf("%d\tPennies\n", pennies);                                            
                                                                                    
       return 0;                                                                    
   }                                                                                
You can find a more elegant version here


3. (20 points) What does the following code produce ?

   #include <stdio.h>
   int main()
   {
       int row = 0, col = 0, space = 0;
       for(row = 0; row < 3; row++)
       {
           for(col = 0; col < 3; col++)
           {
               for(space = col; space > row; space--)
                  switch( col )          
                  {                      
                      case 0:            
                      case 2:            
                          printf("_");   
                          break;         
                      default:           
                          printf("+");   
                          break;         
                  }                      
               printf("X");
           }
           printf("\n");
       }
       return 0;
   }


   X+X__X
   XX_X
   XXX


4. (15 points) The following code contains at least 5 syntax errors. Identify the line number and the error.

 1. #include stdio.h                          
 2. int main                                  
 3. {                                         
 4.     int index, limit;                     
 5.     float value;                          
 6.     printf("Enter limit: ");            
 7.     scanf('%d', &limit);                    
 8.     for(index = 0; index < limit; index++)  
 9.     {                                     
10.        value == 3.14 * index;             
11.        printf("%f\n", value % 3);       
12.     }                                     
13.     return 0;                                
14. }                                         
  1. Line 1:__    Error: you need < and > around stdio.h
  2. Line 2:__    Error: you need () following main
  3. Line 7:__    Error: the first argument of scanf needs to be a string (in double quotes not single quotes)
  4. Line 10:__  Error: == is the equality operator, you need to use the assignment operator =
  5. Line 11:__  Error: value is a float and the mod operator (%) can be applied only to integers.


5. (15 points)

i. What is the value of the following expression (true or false) ? true
  (9 <= 2) || (3 > 5) || (3 == 3) && (9 >= 2) false || false || true && true
     
ii. What is the value of variable a after the following assignment ? 1356
  a = 13 * (1357 / 13) + (1357 % 13 - 1); (1357 - 1357 % 13) + (1357 % 13 - 1)
     
iii. How many times does the following loop execute ? 11
  for(i = 10; i >= (33 % 11); i--) ; [10 9 8 7 6 5 4 3 2 1 0]
     
iv. Write the following as a boolean expression. (A > 0) && (A <= 100)
  0 < A <= 100  
     
v. What is the value of total at the end of the following code fragment ? 2
 
   int total = 0, index = 1;
   do
   {
      index--;
      total += 2;
   } while( index );