Sale!

Project [3]: Loops

$30.00

Category:

Description

Rate this product

Project [3]: Loops

Project Goals
The goals of this project are to:
1. Get students familiar with computing iteration using while and for loops
2. Show students how simple it can be to implement complicated-looking programs
Important Notes:
1. Formatting: Make sure that you follow the precise recommendations for the output content and
formatting: for example, do not change to text in the first problem from “Enter a number: ” to “Enter
number: ”. Your assignment will be auto-graded and any changes in formatting will result in a loss in the
grade.
2. Comments: Header comments are required on all files and recommended for the rest of the program.
Points will be deducted if no header comments are included.
3. Restriction: The use of goto statements anywhere within this program is prohibited. Points will be
deducted if goto is used.
Problem 1
Write a program to guess the square root of a number. It is possible to calculate the square root of a number (for
example 2) by starting with an initial guess and iteratively improving the guess with a simple mathematical
operation until the guess is “good enough.” In the case of the square root, the pseudocode to find the square root of a
number, n, is:
new_guess <- (old_guess + (n / old_guess)) / 2.0
For example, if we want to find the square root of two using this rule, we start with an initial guess of 1.0 and
repeatedly use the rule in a loop to produce the sequence:
1.0, 1.5, 1.41667, 1.41422
The last number in the sequence is very close to the square root of 2. The procedure stops when the square of the
guess is close enough to the number entered. How close is close enough? It depends on the application, but for our
purposes, if the absolute value of the difference between the input and the squared guess is smaller than 1e-5 then
we should consider the guess “good enough.” You should use double data type for all variables used in your
program.
Write a program that asks the user to enter a positive number. Using the procedure described in the previous
paragraph, your program should use a loop to compute the square root of the given number. Your program should
use a while loop to achieve this. The loops should stop when the absolute value of the difference between the
square of the guess and the input number is smaller than 1e-5, as described in the pseudocode below:
Loop ends when: |(guess)2 – n| < 1e-5
Your program should print the value of the guess at each iteration. When you print the guess, you should have a
minimum of 10 spaces in what you print, and you should print five numbers after the decimal point. For the final
answer, you should print five numbers after the decimal point.
The program should function as follows (items underlined are to be entered by the user):
Enter a number: 361
1.00000
181.00000
91.49724
47.72136
27.64305
20.35120
19.04486
19.00005
Estimated square root of 361.00000: 19.00000
Notes:
• For this program, in order to compute absolute values you must use the fabs()function from the math
library. To do this:
• Add #include <math.h in your sq_root.c file
• Add -lm to the compilation command: gcc -o sq_root sq_root.c -lm
• To read and print doubles with scanf and printf, you will need to use %lf
Save your program as sq_root.c
Challenge for problem 1 (10 extra credit points):
We had to use double-precision floating point numbers for this problem because if you use single-precision float
point numbers then the procedure described above may sometimes fail to find a solution. The way that it fails is that
the program gets “stuck” repeating the same guess over and over. For the challenge problem, change all your
double variables into float variables, and add an int variable that counts the number of times you have gone
through the body of your while loop, starting at zero. Your program should print both the iteration counter and the
guess at each step. Your program should be able to detect the “stuck” case and terminate properly.
The program should function as follows (items underlined are to be entered by the user):
Enter a number: 100
0 1.00000
1 50.50000
2 26.24010
3 15.02553
4 10.84044
5 10.03258
6 10.00005
Estimated square root of 100.00000: 10.00000
The iteration counter column and the guess column should be separated by a tab. When you print the guess, you
should have a minimum of 10 spaces in what you print, and you should print five numbers after the decimal point.
For the final answer, you should print five numbers after the decimal point.
Save your challenge separately as sq_root_c.c
Problem 2
Write a program that asks the user to enter an integer number, n, and computes the following mathematical series:
S = 12 – 22 + 32 – 42 +…+ (-1) n+1 * n2
Your program should use a for loop to compute this series.
The program should function as follows (items underlined are to be entered by the user):
Enter an integer number: 5
The value of the series is: 15
Save your program as series.c
Grading Rubric
Grading will be done for each problem as follows:
Correctly-named file 5%
Header comment 2%
Program compiles 5%
Correctly-reading data from terminal 18%
Correct result printed 20%
Submission details
To submit your project, you will have to use the submission script. You do this by either:
1. Working on an ECC machine
2. Working on the provided VMware
3. Secure Copying your files (See Mac Support for information)
To Submit your project:
• Have a directory called “project3”
• Save your *.c files in that directory
• To submit: (don’t type the ‘’ symbols)
cd project3
submit
The submission script copies all files in the current directory to our directory. You may submit as many times as you
like before the deadline, we only keep the last submission.