CS1101C Lab 2 (Even Week)

Newton’s Method

The deadline for this lab question is Friday 12 October 2007, 23:59:59 hours.

The name of your C program file must be called cubic1.c, files with any other name will not be marked.

Preliminary

For this lab, you will be asked to used your deductive and inference skills. We will present a sample data file (text input file) and a sample run on the data file. Study both the sample data file and sample run carefully, and write an equivalent working program which reads the data from the sample data file and produces the required output as shown in the sample run.

Background

Please refer to Wikipedia for more information and an example on using Newton’s method.

Data File

Assume that the sample data file (text input file) is always called cubic1.txt. The following are the contents of this sample data file. Study it carefully.

1 -6 11 -6 2.6
2 -6 12 -7 -3.7
-3 0 5 0 -2

Sample Run

Assuming that the executable is cubic1, a sample run of the program is shown below. User input is denoted in bold. Follow the sample run precisely. Study the sample run carefully in relation to the sample data file given above.

$ gcc -Wall -lm cubic1.c -o cubic1

$ ./cubic1

Solving the following Cubic Equation:
    +1x^3 -6x^2 +11x -6 = 0
    where x0 = 2.600000
One solution for x = 3.000000

Solving the following Cubic Equation:
    +2x^3 -6x^2 +12x -7 = 0
    where x0 = -3.700000
One solution for x = 0.834835

Solving the following Cubic Equation:
    -3x^3 +0x^2 +5x +0 = 0
    where x0 = -2.000000
One solution for x = -1.290994

$

Note and Ponder

  1. Assume that all user input is valid. No error checking is required.

  2. Each line in the data file has four integers followed by a floating point number. The number of lines is unknown but it is always at least one. Assume that the first integer in each line is always non-zero. Your program must work for other data files.

  3. To compare floating-point values to the value zero, use the “close to zero” definition given in the lecture notes and the textbook, i.e. a value is “close to zero” if its absolute value is < 0.0001 (the threshold). For this lab, set the threshold to 0.00001.

  4. If your program does not work as you expect (logical errors), use extra printf statements to print out all the values of your variables to aid in your debugging.

  5. Most importantly, have lots of fun programming!


This document, index.html, has been accessed 14 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

A total of 7 different hosts have accessed this document in the last 445 days; your host, 216.73.216.39, has accessed it 1 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.