C programming help. 1. Overview Foe tis assigament you will write a program that
ID: 3750881 • Letter: C
Question
C programming help. 1. Overview Foe tis assigament you will write a program that will determine the sqaare root of a number entered by then The epts used in this progra" include loops, conionals, tandem number gmerton, sol as prief & KnAd variables, and using the math library 2. More Specifics To deermine the square root of a number, you will implement the method knewn as the Bahylonian mehed wch was wsd by the Babylonian people of Mesopotamia maybe as carly as 1900 BC. The Babylonian methed isvelves making a guess foe what the square root of a number might be dividing the umberby the guess, aking the averape of those two numbers, and then using thn avcrape as the nest gucss This method can be epresented by the ellowing ormula guess", guess, 4 (N/ ess.)) " It turns oun that the sequence produced convenges very quickly to the square root N Write a program called findsquazeoot.e Prompt the uner te enter a number So which the square noot will be determinod Use a random sumber goneator to gencrate a guess to start with that will be between I and hehor b the user. Write a loop that uses the abave formula, kecping track of the number of scrations, to determine the squanc noot o thut number Show the value entesed by the user, cach value ef he new rosult and how marty stcps it ook te get to the squne oet Have your program do this 3 times with 3 random guesses for the number emmered by the user. The ouput ldl sinilar te the following (user ingut in bold Enter a nunber: 72 Using the nath library, the square root of 72.0000 is 8.485281 step 1: step 2: step 3 step 4: step 5: step 6i Using the Babylonian method, the square root of 72 with a starting goess of 2 was found in 6 steps 26.692308 14-694857 9.797265 8.573127 8-48573 8.485281 571429 8.546908 8.485504 48528 step 1: step 2: step 4 Using the Babylonian method, the square root of 72 with a starting goess of 34 vas found in 4 steps step li step 21 step 3 step 41 step s: step 6: Uaing the Babylonian method, the square root of 72 with a atarting goess of 57 vas found in steps 9-131579 15.801562 10.17903 8-626199 8.486432 8.48528 The namber ef steps (should) vary depending en the starting puess valae Values for the inpus and intermediase resuls should be doubles, as well as the squase o. The sqrto fanction from the math.h library should be used to how before the slaps what the squarc oot is But the calculatiee at cach sip should accomplished with the formela shown above on the fisi page. You can see the value at the end of the lep docs in fact mnch the value frem using the sqrt1) function Whem you compile a program that wses any of the math fanctions from the math.h lihrary, you must incdethe-In flag Fer esample cc findsquazeoot.e-1 Notice that the valurs shown in each of the three runs that use the Habylonian method line up in two columns You shol format your outpat similarly. To msure that your columts remais propaly alignod, your sccond celum sbould have awds 1S. To view eutput fomatting placcholder frems chapeer 3 and lab 4: [flagslIwidth).preciaion [iengthi type where the width repeesenes the miniman number ef characters to generane padding with leading spaces
Explanation / Answer
ScreenShot
---------------------------------------------
Program
//Header files for I/O,math operation(squire root) and rand generator
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int main()
{
//Variables for input and calculation
double num, guess = 0, temp, temp1, convergence = 0.000001;
int stepCount = 0, i, j;
//User prompt
printf("Enter a number:");
scanf("%lf", &num);
//Display using sqrt function
printf("Using the math library, the squire root of %lf is %lf ",num,sqrt(num));
//Loop for 3 times display of root using Babylonian
for (i = 0; i < 3; i++) {
//variable for step count display
j = 1;
//Variable for number of steps
stepCount = 0;
//Generate random number for guess
guess = rand() % int(num) + 1;
//Save start guess for display
temp1 = guess;
//Save enterd number into temp for calculation
temp=num;
//Loop until convergence value
while (temp-guess>convergence)
{
//Square root in each step
temp = (temp + guess) / 2;
//Display root
printf("Step %d: %lf ",j,temp);
//Change guess value
guess = num /temp;
//Count steps
stepCount++;
j++;
}
//Display the number of steps to find root
printf("Using the Babylonian method, the squire root of %d with a starting guess of %d found in %d steps. ", int(num), int(temp1), stepCount);
}
return 0;
}
-----------------------------------
Output
Enter a number:72
Using the math library, the squire root of 72.000000 is 8.485281
Step 1: 57.000000
Step 2: 29.131579
Step 3: 15.801562
Step 4: 10.179037
Step 5: 8.626199
Step 6: 8.486432
Step 7: 8.485281
Using the Babylonian method, the squire root of 72 with a starting guess of 42 found in 7 steps.
Step 1: 54.000000
Step 2: 27.666667
Step 3: 15.134538
Step 4: 9.945934
Step 5: 8.592537
Step 6: 8.485951
Step 7: 8.485281
Using the Babylonian method, the squire root of 72 with a starting guess of 36 found in 7 steps.
Step 1: 71.500000
Step 2: 36.253497
Step 3: 19.119756
Step 4: 11.442747
Step 5: 8.867471
Step 6: 8.493518
Step 7: 8.485285
Step 8: 8.485281
Using the Babylonian method, the squire root of 72 with a starting guess of 71 found in 8 steps.
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.