Laboratory Exercises For Computer Science 213

Lab

Lab

Goals: This laboratory exercise provides practice with basic elements of the C programming language. Specifically, the lab emphasizes conditional (if) and looping (for, while) constructs.

Problems: In this lab, you are to write solutions to the following two problems. In each case, turn your solutions utilizing the format for submitting assignments.


  1. Solving Quadratic Equations: Write a program that reads real numbers a, b and c, and prints the solution(s) to the equation ax2 + bx + c = 0, if solutions exist. The program also should print the number of solutions present.

    In particular, your program should distinguish the following cases:

Notes:


In the following problem, use the following C format to define a function f(x)of a real parameter x that returns a real result:


double f (double x) {

}
  1. Bisection Method for Approximating Roots: Write a program that uses the bisection method to approximate a root of a function. In particular, the program should have the following characteristics:
    1. A function f(x) should be defined separately, as noted above.
    2. Endpoints a and b should be defined as given constants.
    3. An accuracy epsilon should be defined as a given constant.
    4. The program should check initially that f(a) and f(b) have opposite signs, and the program should report an error if this pre-condition is not met.
    5. Assuming f(a) and f(b) have opposite signs, use the Bisection Method to approximate a root of the function that occurs between a and b to an accuracy within epsilon.
    6. In applying the Bisection Method, the program should print, in table form, the current interval in which the root is located through each iteration.

    Using the notation above, the Bisection Method proceeds as follows for an iteration:

    In the second and third cases, the interval containing the root has been cut in half, from [a, b] to [a, c] or [c, b]. For the next steps, the above process is repeated, with the interval containing the root being halved each time. Once the root has been trapped in a very small interval (smaller than epsilon), the midpoint of that interval provides an approximation to the root.

    Example: The square root of 2 can be approximated as a root of the function f(x) = x2 - 2. If we start with [a, b] = [1, 2], and continue until the length of the interval is less than 0.01, we might obtain the following output:

    
    left endpoint      right endpoint
       1.000000           2.000000
       1.000000           1.500000
       1.250000           1.500000
       1.375000           1.500000
       1.375000           1.437500
       1.406250           1.437500
       1.406250           1.421875
    
    Since the last subinterval has length 0.0078125, less than the 0.01 required, the midpoint 1.14196875 of the last interval is the desired approximation. (Note: This agrees with the true value of the square root of 2, which is 1.141431, to two decimal places.)


This document is available on the World Wide Web as

     http://www.math.grin.edu/~walker/courses/213.fa98/lab-intro-to-c.html

created August 31, 1998
last revised September 1, 1998