BU CAS CS 113
Introduction to Computer Science II with Intensive C
Fall 1997


MIDTERM REVIEW MATERIALS


Last Modified: Mon Nov 17 00:30:50 1997

This page contains information which may be useful as you prepare for the midterm, which will be given in class on Thursday, October 23. I may add information to this page prior to the midterm, so check here occassionally and look at the last revision date above to see if anything has been changed since you last looked at the page.

This page contains

Review Questions from Programming Abstractions in C

The following Review Questions in the text are useful for preparing for the midterm. Note that the content is more important than the format of the questions and that this is not an exhaustive list of material to study.

Additional Questions and Programming Exercises

  1. Give the function header for each of the following.

  2. Label the elements of a 5-by-3 two dimensional array SALES to indicate the order in which they are set to zero by the following program segment.
     
          for (row = 0; row <= 2; row++)
            for (column = 0; column <= 4; column++)
              SALES[column][row] = 0;
    
  3. What does the following program do?
       #include 
       #include "genlib.h"
       #include "sipmio.h"
    
       int Mystry2 (const char *);
    
       void main( void )
       {
        string line;
        printf("Enter a string: ");
        line = GetLine();
        printf("\n%d", Mystry2(line));
       }
       
       int Mystry2 (const char *s)
       {
        int x = 0;
        for ( ; *s != '\0'; s++)
           x++;
        return x;
       } 
    
  4. What does the following program do?
       #include 
       #define SIZE   4
    
       int WhatIsThis (int [], int);
     
       void main( void )
       {
        int a[SIZE]={1,2, 3, 4};
        printf("\nThe result is: %d", WhatIsThis(a, SIZE));
       } 
    
    
       int WhatIsThis (int b[], int num)
       {
        if (num == 1)
          return b[0];
        else
          return b[num -1] +  WhatIsThis(b, num - 1);
       }
    
  5. Given the equation
                         3
                   y = ax + 7
    
    which of the following, if any, are correct C statements for this equation? Why?
            a)  y = a * x * x * x + 7;
            b)  y = a * x * x * (x + 7);
            c)  y = (a * x) * x * x + 7;
            d)  y = a * (x * x * x) + 7;
            e)  y = a * x * (x * x + 7);
    
    Which of the correct ones, if any, is clearest?

  6. What does the following program do, if the user inputs two numbers 6 and 9 separated by a space.
       #include 
       main()
       {
        int i, j, x, y;
        printf ("enter two integers : ");
        scanf("%d %d", &x, &y);
        for (i=1; i<= y; i++){
          for (j=1; j<= x; j++)
            printf("@");
          printf("\n");
        }
       }
    
    [A note on I/O: On the midterm, you will not be required to write code which uses scanf (although you may). You should be able to read scanf in its simplest forms, as in this example. For file I/O, it will suffice to use fprintf() and getc(), putc(), ungetc(). You may use the functions in simpio.h in any code you write.]

  7. Write a function that takes a binary number (represented as a string of 0's and 1's, possible with a -sign in front) and returns the integer to which it is eqivalent. Your program should take care of sign of the number. If the input "number" is negative, the final decimal number should also be negative. You can assume that the number will be entered with no spaces. If the string is not a valid binary representation, the function should return 0.

  8. Write a function which takes as argument the name of a file and and prints to the screen the contents of the file with the following modifications: it should convert lower case letters to upper case, and vice-versa, tabs should be printed as 3 spaces.

  9. A number n is said to be a perfect number if it is equal to the sum of all its divisors, excluding itself. For example, the divisors of 6 are 1, 2, and 3. Since 6 = 1 + 2 + 3, it is a perfect number. Write a function called IsPerfect that takes an integer as argument and returns 1 if it is a perfect number and 0 otherwise. Write a main program that reads in a number n and prints all perfect numbers between 1 an n by using the function IsPerfect.

  10. Which of the following you can NOT use as identifiers and why?
        4th        star*       scanf      one-to-1
    
  11. What is the minimum size of a character array to store the following string:
                "\t\"This is a string\"\n"
    
  12. What gets printed? Explain. (If you need, you can assume the address of i to be 1000.)
    	int i = 3, *p;
            p = &i;
    
    	printf( "%d %d %d %d\n", p, *p + 7, **&p, p - (p-2));
    
  13. Which statements are legal, which are not, why and why not?
    	int i = 9, a[3], *p;
    	char b[4], *q;
    
    	a = p;
    	p = a;
    	q = &i;
    	q = b;
    	q = b[2];
    	q = &b[2];
    	q = b + 2;
    	q = &b[0] + 2;
    
  14. Write functions to draw each of the following pictures. Start by writing a function that prints the n-th line of the object and use it to print the picture required. (Use for-loops, while, switch.)
    	a) Parallelogram:
    				********
    			       ********
    			      ********
    			     ********
    
    	b) Pyramid:
    				0
    			       *1*
    			      ++2++
    			     $$$3$$$
    			    ****4****
    			   +++++5+++++
    			  $$$$$$6$$$$$$
                                 ......
    
  15. Write a function that returns the sum of the first n odd numbers.

  16. Write a function which takes a string argument giving the name of a text file and returns the number of punctuation characters there are in the it.

  17. What is printed by the following piece of code?
    	void main( void )
    	{
    	  int x;
    	  int a[10] = {12, 11, 10, 9, 8, 7, 6, 5, 4, 3 };
    	  int *p;
    
    	  p = &x;
    	  x = 5;
    	  *p = 8;
    	  p = a + 5;
    	  printf( "%d, %d\n", x, *p );
    	}
    

  18. What is the definition (used in this class) of the logarithm? Based upon the algorithms we have seen, when does the logarithm enter into the computational complexity of an algorithm?
  19. What is the computational complexity of each of the sorting algorithms we have studied in class (best case, worst case, average case). For algorithms where best case, worst case and average case are not the same, in what situations do best and worst case occur?
  20. Given an array containing 8 elements: 9, 5, 3, 10, 2, 6, 8, 12, trace the execution of each algorithm. For the recursive algorithms, what are the first level recursive calls?
  21. In the partitioning step of Hoare's QuickSort algorithm, one imagines repeatedly first moving one's right hand (until what happens?) and then the left. Eventually the two hands meet. What happens if you move the left hand first?
  22. How does the choice of pivot affect the efficiency of QuickSort?
  23. What is a hybrid sort? Why might a hybrid sort be even better than QuickSort?