CS 2073 Section 2 Spring 2007 First 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. The format specifier "%f" can be used with printf to print a double value.
      T
      
    2. The format specifier "%f" can be used with scanf to read a double value.
      F
      
    3. An if statement must have a corresponding else statement.
      F
      
    4. A switch statement must have a default case.
      F
      
    5. Anything that is not zero is considered to be "true" as a logical value.
      T
      
    6. 'a' < 'z'
      T
      
    7. It's possible to rewrite any switch statement using if statements instead of switch/case.
      T
      
    8. It's possible to rewrite any while statement using a for statement instead.
      T
      
    9. Any C program can be written without using while.
      T
      
    10. Any C program can be written without using ++.
      T
      
    11. The sin function is declared in <math.h>.
      T
      
    12. You must compile with -lm to use the printf function.
      F
      
    13. A function in C doesn't have to return a value.
      T
      
    14. When a parameter is changed inside a function, the corresponding argument is also changed.
      F
      
    15. A bit is 8 bytes.
      F
      
  2. Write a program that prompts the user for an angle in degrees and prints the equivalent angle in radians. Recall that there are 360 degrees in a circle, and 2π radians in a circle (π is approximately 3.14159). Your program should be correct.
    #include <stdio.h>
    
    int main () {
            double d;
    
            printf ("Enter degrees: ");
            scanf ("%lf", &d);
            printf ("%f radians\n", 3.14159 * (d / 180));
            return 0;
    }
    
    
  3. 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.
    #include <stdio.h>
    
    int main () {
            int     i;
    
            for (i=1; i<=99; i+=2) printf ("%d\n", i);
            return 0;
    }
    
  4. Consider the following C program. It is supposed to read a list of numbers from the user until the user types -1; then it should print the average of the numbers. It won't work for several reasons. Describe four of the errors in the program. Don't give any more than four; four right answers and one wrong answer still means points off.

    #include <stdio.h>
    #include <stdlib.h>
    
    int main () {
            double  sum, avg, num
            int     n;
    
            n = 0;
            printf ("Enter some numbers, -1 when done.\n");
            scanf ("%f", &num);
            while (num != -1) {
                    sum += num;
                    n++;
                    scanf ("%lf", &num);
            avg = sum / n;
            printf ("The average is %f\n", avg);
            exit 0;
    }
    
    Some errors:
    1.  Semicolon is missing from the end of the first 'double' declaration.
    2.  First scanf should use the "%lf" format to read a double.
    3.  The while statement has an open brace without a close brace.
    4.  The value of 'n' could be zero causing a division by zero error.
    5.  The exit function should have parentheses around its argument.