Be sure you have read (or at least skimmed) the assigned readings from chapter 6.
Do the following programming problems. You will end up with at least one code file per problem. Submit your program source (and any other needed files) by sending mail to bmassing@cs.trinity.edu with each file as an attachment. Please use a subject line that mentions the course and the assignment (e.g., ``csci 1312 hw 5'' or ``CS1 hw 5''). You can develop your programs on any system that provides the needed functionality, but I will test them on one of the department's Linux machines, so you should probably make sure they work in that environment before turning them in.
Newton's method for computing the square root of a non-negative number starts with an initial guess and then repeatedly refines it using the formula
(1) | |||
(2) | |||
(3) | |||
(4) | |||
(5) | |||
(6) | |||
(7) |
Write a C program that implements this algorithm and compares its results to those obtained with the library function sqrt(). Have the program prompt for , the threshold value, and a maximum number of iterations; do the above-described computation; and print the result, the actual number of iterations, the square root of as computed using library function sqrt(), and the difference between the value you compute and the one you get from sqrt(). Also have the program print an error message if the input is invalid (non-numeric or negative).
Here are some sample executions
(assuming you call your program newton.c and compile with
make):
[bmassing@dias04]$ ./newton enter values for input, threshold, maximum iterations 2 .0001 10 square root of 2: with newton's method (threshold 0.0001): 1.41422 (3 iterations) using library function: 1.41421 difference: 2.1239e-06 [bmassing@dias04]$ ./newton enter values for input, threshold, maximum iterations 2 .000001 10 square root of 2: with newton's method (threshold 1e-06): 1.41421 (4 iterations) using library function: 1.41421 difference: 1.59472e-12
Hints:
Write a C program that gets a positive integer from the user and prints an by pattern of stars and spaces like the following:
For :
************ ** ******** **** ****** ****** **** ******** ** ************
For :
****************** ** ************** **** ************ ****** ********** ******** ******** ********** ****** ************ **** ************** ** ******************
Print an error message if what was entered is not a positive integer.
You might find it useful to split your program into several functions, as a way of keeping the main program from being too complicated, and also as a way of not writing similar code over and over. Functions you might find useful in addition to the main one:
Include the Honor Code pledge or just the word ``pledged'', plus at least one of the following about collaboration and help (as many as apply).1Text in italics is explanatory or something for you to fill in. For programming assignments, this should go in the body of the e-mail or in a plain-text file honor-code.txt (no word-processor files please).
Include a brief essay (a sentence or two is fine, though you can write as much as you like) telling me what about the assignment you found interesting, difficult, or otherwise noteworthy. For programming assignments, it should go in the body of the e-mail or in a plain-text file essay.txt (no word-processor files please).