CS 2073-001,   Final Exam, Spring 2006 Answers

Points for each problem: 1-15, 2-15, 3-20, 4-20, 5-20, 6-20, 7-20, 8-20.


  1. Write a complete C program that will use scanf to read values for three double variables a , b , and c . These values are assumed to be the coefficients of a quadratic equation a*x2 + b*x + c = 0 . Your program should compute the double d = b2 - 4*a*c . Then an if-else should check and print either "real, not equal", "real, equal", or "complex", depending on whether the value of d is greater than zero, equal to zero, or less than zero. (Do not actually calculate the roots.)


  2. Write an if or if-else statement that will check if a variable int size is not inside the range from 2 to 10 (inclusive) and will print "Out of range" if this is so. (If the variable is in the range from 2 to 10 (inclusive), your statement should print nothing. Just give the statement and not a complete program.)


  3. loops: Consider the following program fragment that involves a for loop.

    1. Say exactly what this loop will print.

    2. rewrite this loop as a while loop that will print exactly the same thing as the for loop.

        #include <stdio.h>
        
        int main() {
           int k;
           for (k = 5; k >= 1; k--) {
              printf("%i", k);
              if (k != 1) printf(", ");
           }
           printf("\n");
           
           k = 5;
           while (k >= 1) {
              printf("%i", k);
              if (k != 1) printf(", ");
              k--;
           }
           printf("\n");
        }
        
        Output of a run
        5, 4, 3, 2, 1
        5, 4, 3, 2, 1
        


  4. Series: Write a code segment that will calculate the sum of n terms of the following series:

    Notice that the signs are alternating. Start numbering the terms with 1 and assume that n is even, so that the sign of the last (nth) term is negative. Your program segment must use a loop to calculate the sum. It must print the sum at the end.


  5. functions: Write a function named circle that will take two doubles as input formal parameters. This function should decide whether the point (x, y) lies inside the unit circle (the circle with center at the origin and radius 1), that is, if x2 + y2 < 1. In case the point is inside the unit circle, return 1, and otherwise return 0. You need to supply code below for the function and its prototype so that it fits into the code given:


  6. Arrays: Given the two arrays a and b below, use a loop to let c be the sum of a and b, that is, each element of c is the sum of the corresponding elements of a and b. Then print the elements of c. (You must use loops for both parts.)


  7. Arrays as parameters: Write a function maximum with parameters a 1-dimensional array of integers and the size of the array. This function should find the maximum of the array elements (the largest value in the array). maximum should return this number as an int. For example, the main function below should print 98. Your function must use a loop and must work for an array of any size greater than 0.


  8. Input/Output: Consider the following program, similar to two examples from class, but simpler. The comments at the beginning of each line give a line number so that I can asks questions about the program.

    1. If you run this program, what will the black box show? Nothing except for the standard message: Press any key to continue.

    2. Say briefly what lines 8-12 are doing. Opening the file "source.txt" for reading, and checking if it couldn't be opened.

    3. Say briefly what lines 13-19 are doing. Reading integers from the file, and putting them into the array s. n is the number read. A -1 terminated the input. Finally, line 19 closes the file so that something else can be done with it.

    4. Say briefly what lines 21-22 are doing. Sticking one more integer (99) into the array, and making n one bigger.

    5. Say briefly what lines 24-28 are doing. First opening the same file for writing. Since the file exists, its contents will be thrown out. Then the numbers in the array, including the extra 99 at the end, will be written to the file that was just emptied. Finally a -1 is written at the end. (There was no -1 in the file.)

    6. If the file "source.text" looks like:

        90 87 56 78 -1
        

      what will it look like after the program has run?

        90 87 56 78 99 -1
        

    7. Suppose your run the program one more time. Now what will the file "source.text" look like?
        90 87 56 78 99 99 -1