Sample Midterm A22.0002


1. (25 points) Write a program that translates an 8-digit binary number to it's decimal equivalent.

Sample run:

   Enter base 2 number: 10110011
   Decimal equivalent : 179

Algorithm

  1. Set the variable result to 0.
  2. Multiply result by 2.
  3. Read next digit of input number and add to result.
  4. Repeat steps 2 and 3 eight times.
  5. Display result.
Hints, hints, hints:


   #include <stdio.h>                               
                                                    
   int main()                                       
   {                                                
       int result = 0, index = 0;                       
       char digit;                                  
                                                    
       printf("Enter base 2 number: ");             
                                                    
       for(index = 0; index < 8; index++)           
       {                                            
           result *= 2;                             
           digit = getchar();                       
           result += digit - '0';           
       }                                            
                                                    
       printf("Decimal equivalent:  %d\n", result); 
                                                    
       return 0;                                    
   }                                                

Extra credit (5 points) What would you do to modify the previous program to handle numbers in any base 2 through 9 ? Your new version must ask the user for the base of the number.

Sample run:

   Base of number     : 4
   Enter base 4 number: 30321012
   Decimal equivalent : 52806


  1. Create an integer variable base.
  2. Read base from keyboard.
  3. In step ii of the algorithm, instead of multiplying by 2, multiply by base


2. (25 points) Superman read of Zorro's program to produce Zs and became jealous: he wanted a similar program to produce his Ss that and you drewn in that assignment. The program should ask for the width of the letter and then display an S that is roughly the same height as it's width (depending if the width is even or odd, the height might be one greater than the width). Sample run:

   Enter width: 8
   SSSSSSSS
   S
   S
   S
   SSSSSSSS
          S
          S
          S
   SSSSSSSS

Hints, hints, hints:


   #include <stdio.h>
                                                                                    
   int main()                                                                       
   {                                                                                
       int width = 0, index = 0, space = 0;                                         
                                                                                    
       printf("Enter width: ");                                                     
       scanf("%d", &width);                                                         
                                                                                    
       /* Top horizontal line */                                                    
                                                                                    
       for(index = 1; index <= width; index++)                                      
       {                                                                            
           printf("S");                                                             
       }                                                                            
                                                                                    
       /* Go to new line */                                                         
                                                                                    
       printf("\n");                                                                
                                                                                    
       /* First Vertical line */                                                    
                                                                                    
       for(index = 1; index <= (width / 2 - 1); index++)                            
       {                                                                            
           printf("S\n");                                                           
       }                                                                            
                                                                                    
       /* Middle horizontal line */                                                 
                                                                                    
       for(index = 1; index <= width; index++)                                      
       {                                                                            
           printf("S");                                                             
       }                                                                            
                                                                                    
       /* Go to new line */                                                         
                                                                                    
       printf("\n");                                                                
                                                                                    
       /* Second vertical line */                                                   
                                                                                    
       for(index = 1; index <= (width / 2 - 1); index++)                            
       {                                                                            
           /* move over enough white spaces */                                      
           for(space = 1; space <= (width - 1); space++)                            
           {                                                                        
               printf(" ");                                                         
           }                                                                        
           printf("S\n");                                                           
       }                                                                            
                                                                                    
       /* Bottom horizontal line */                                                 
                                                                                    
       for(index = 1; index <= width; index++)                                      
       {                                                                            
           printf("S");                                                             
       }                                                                            
                                                                                    
       /* Go to new line */                                                         
                                                                                    
       printf("\n");                                                                
                                                                                    
       return 0;                                                                    
   }                                                                                


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

   #include <stdio.h>

   int main()
   {
       int ind1 = 0, ind2 = 0;
    
       for(ind1 = 0; ind1 <= 3; ind1++)
           for(ind2 = ind1; ind2 <= 4; ind2++)
               printf("%c", 'a' + ind2);
       printf("\n");
    
       return 0;
   }


   abcdebcdecdede


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.    const int limit = 5;
 5.    int index = 0; value = 0; total = 0;
 6.    limit = 3;    
 7.    for(index = 1; index <= limit; index++)
 8.    {
 9.       printf("%d :", index);
10.	  scanf("%d", value);
11.       total += value;
12.   printf(Total = %d\n, total);
13.   return 0;
14. }
  1. Line: 5__    Error: separate list of variables with "," not ";".
  2. Line: 6__    Error: cannot reassign value to const.
  3. Line: 10_    Error: need & before variable in scanf.
  4. Line: 12_    Error: missing closing brace (}).
  5. Line: 12_    Error: need double quotes around first argument of printf.


5. (15 points)
i. What is the value of the following expression ?  
  (9 / 2) + (9 % 3) 4__
   
ii. How many times does the following loop execute ?  
  for(I = 1; I >= (13 % 3); I--) 1__
   
iii. How many times does the following loop execute ?  
  for(I = 'a'; I <= 'd'; I++) 4__
   
iv. Given the following code fragment:  
 
   total = 0;
   for(index = 0; index < 100; index *= 2)
   {
      total += index;
      if( index == 5 )
      {
         break;
      }
   }
   
 
  What will is the final value of total: 0__
   
v. Given the following code fragment:  
 
   total = 5;
   total *= total + (total % 3); 
   
 
  What is the final value of total: 35_