CS 2073 Section 2 Spring 2007 Second Exam

Write all of your answers on the blank paper provided. Do not write on this question paper. Make sure to put your name on each sheet of paper you turn in. Make sure to write clearly; points will be taken for illegible handwriting.
  1. True or False. Write 'T' for True and 'F' for False for each of the following statements:
    1. A recursive function cannot include a for loop.
      Answer:
      F
      
    2. The fopen function returns 0 if the file does not exist and the second argument to fopen is "r".
      Answer:
      T
      
    3. The first argument to fscanf is a format string.
      Answer:
      F
      
    4. Using "bubble sort" to sort n items takes a number of comparisons proportional to log n.
      Answer:
      F
      
    5. Binary search is strictly superior to linear search; there is no reason why we would ever need to use linear search except possibly because the code is easier to understand.
      Answer:
      F
      
    6. The index of the last element of an array of 100 integers is 100.
      Answer:
      F
      
    7. Strings are represented in C by arrays of null-terminated characters.
      Answer:
      T
      
    8. main208 is the only computer you are allowed to use for this class.
      Answer:
      F
      
    9. The first element of the argv array in main is the name of your program.
      Answer:
      T
      
    10. fclose is a function that performs a "float conversion long operating system execute."
      Answer:
      F
      
  2. What is the output of the following program?
    #include <stdio.h>
    void foo (int n) {
            if (n != 0) {
                    foo (n-1);
                    printf ("%d\n", n);
            }
    }
    int main () {
            foo (10);
            return 0;
    }
    
    Answer:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
  3. What is the output of the following program?
    #include <stdio.h>
    double f (double x) {
            return x * x;
    }
    int main () {
            double  dx = 0.000001;
            double  x, sum;
    
            sum = 0.0;
            for (x=0; x<=3; x+=dx) {
                    sum = sum + f(x) * dx;
            }
            printf ("%0.3f\n", sum);
            return 0;
    }
    
    Answer:
    9.000
    
  4. Write a C program to print the odd numbers between 1 and 99 inclusive. Your program should be correct and should not take more than 8 lines of C code.
    Possible answers:
    
    /* this version checks for oddness */
    #include <stdio.h>
    
    int main () {
            int     i;
    
            for (i=1; i<=99; i++)
                    if (i % 2 == 1) printf ("%d\n", i);
            return 0;
    }
    
    /* this version only computes odd numbers */
    #include <stdio.h>
    
    int main () {
            int     i;
    
            for (i=1; i<=99; i+=2) printf ("%d\n", i);
            return 0;
    }
    
  5. Write a C function called find_min that accepts two parameters: an array of integers v and an integer n. The function should return the index of the smallest element of the array.
    int find_min (int v[], int n) {
            int     i, mini;
            
            mini = 0;
            for (i=1; i<n; i++) if (v[i] < v[mini]) mini = i;
            return mini;
    }
    
  6. Write a C function called prime that accepts a single integer argument n. The function should return 1 if n is prime, or 0 if n is not prime.
    int prime (int n) {
            int     i;
    
            if (n < 2) return 0;
            for (i=2; i<n; i++) if (n % i == 0) return 0;
            return 1;
    }