Question

       Write a C++ program that uses the iteration process above to approximate the square...

  
    Write a C++ program that uses the iteration process above to approximate the square root of a positive number a. The value of a will be entered from the console, as well as a tolerence ε. Iteration will continue until |x₁-x₀|<ε, when the program will terminate. The value of x₁ at that point will be declared the appropriate approximation. Output will be to a file that can be imported into SWP. Include the step of the process, e. g., 1,2,3,…, as well as the approximation at that step. Then in SWP, you will arrange the data into two columns, and prepare a final report. Your report will contain the entire text of the C++ program, the output of the program appropriately formatted, as well as your conclusions about the data. Use ε=10⁻⁸ and a=40+ the last two digits of your university X number. Due: Sunday, March 18.

it is iteration process above

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Executable code

#include<iostream>

using namespace std;

double sqrt(double n, double e)

{

// left bound

double p = 0.0;

// right bound

double r = n;

double mid = 0.0;

while( r - p > e )

{

mid = p + ( r - p ) / 2.0;

if( mid * mid > n )

r = mid;

else

p = mid;

}

return mid;

}

int main()

{

cout<<"Enter the number : ";

double n;

cin>>n;

cout<<"Enter the tolerance :";

double e;

cin>>e;

double ans = sqrt(n, e);

cout<<"\nSquare Root :"<<ans;

return 0;

}

Sample out put

gcc version 4.6.3 Enter the number:26 Enter the tolerance:0.09001 Square Root :5.09902

Add a comment
Know the answer?
Add Answer to:
       Write a C++ program that uses the iteration process above to approximate the square...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in C++. Write a function squareRoot that uses the Newton’s method of approximate calcu-lation of the...

    in C++. Write a function squareRoot that uses the Newton’s method of approximate calcu-lation of the square root of a number x. The Newton’s method guesses the square root in iterations. The first guess is x/2. In each iteration the guess is improved using ((guess + x/guess) / 2 ) as the next guess. Your main program should prompt the user for the value to find the square root of (x) and how close the final guess should be to...

  • Write a C program countFiles.c to be executed on the command line as follows: countFiles <directory> The program...

    Write a C program countFiles.c to be executed on the command line as follows: countFiles <directory> The program should count the (regular) files in the specified directory as well as all subdirectories and output the total number on the console. Files and subdirectories whose names .start with should be ignored! To do this, define a function int countFilesRec(char* dirName)that dirName returns the number of (regular) files in the directory and all the subdirectories. Call the function recursively to count the...

  • Write a C++ console application that uses functions to calculate an employee’s salary.

    Write a C++ console application that uses functions to calculate an employee’s salary.PART 1: Create a void function to print a menu asking the user to choose from the following options:1 - Annual salary2 - Monthly salary3 - Daily rate of pay4 – ExitThe void function only prints the menu. Format your cout statement to include the menu as displayed above.If the chosen option is 1- Annual Salary, ask the user for the number of hours they work perweek and...

  • Student ID: 123 Write a C+ program with the following specifications: a. Define a C++ function (name it function_Student...

    Student ID: 123 Write a C+ program with the following specifications: a. Define a C++ function (name it function_StudentlD where StudentID is your actual student ID number) that has one integer input (N) and one double input (x) and returns a double output S, where N S = n 0 and X2 is given by 0 xeVn n 0,1 Хл —{2. nx 2 n 2 2 m2 x2 3 (Note: in the actual quiz, do not expect a always, practice...

  • Write a C++ program that will deliver a multiple choice quiz. The program should Read questions...

    Write a C++ program that will deliver a multiple choice quiz. The program should Read questions from the file questions.txt Read the answer key for each test question from the file answers.txt When the program runs it will present each question and obtain a response After all questions have been answered, the program should output a report with the question number, the response given and either “correct” or “incorrect”. Show the output on the console and store the report in...

  • Create a CPP file for a C++ Program. Write exactly these functions, power(x,y) function and a...

    Create a CPP file for a C++ Program. Write exactly these functions, power(x,y) function and a print(text, number) function and the main() function. The power(x,y) function returns an integer result that is calculated by raising a number x (integer) to a power y (integer). The second argument y (exponent) of the function can not exceed 100. If the second argument exceeds 100, the function should return -1. Your power(x,y) function must be able to take either 1 or 2 integer...

  • Write MATLAB code g(x)=x+a*f(x) f(x)=(e^x)+(x^2)-x-4 function f(x) is stored in fogp1.m (c) (1) Create a file...

    Write MATLAB code g(x)=x+a*f(x) f(x)=(e^x)+(x^2)-x-4 function f(x) is stored in fogp1.m (c) (1) Create a file gopgi.m to calculate the function glir, a) (see Preparation, ex. 7c) function [y] =gopg1(x,a) % input: x, a % output: y y= .....; (2) Create a file sucsub1.m and write a program that calculates the solution using the method of successive substitution. Calculate the values of g(x, a) by making multiple calls to the function gopg1(x, a). Take xo = 1.3 as starting value...

  • Problem: You will write a program a C++ to compute some statistics based on monthly average...

    Problem: You will write a program a C++ to compute some statistics based on monthly average temperatures for a given month in each of the years 1901 to 2016. The data for the average August temperatures in the US has been downloaded from the Climate Change Knowledge Portal, and placed in a file named “tempAugData.txt”, available on the class website. The file contains a sequence of 116 values. The temperatures are in order, so that the first one is for...

  • Use C++ For this week’s lab you will write a program to read a data file...

    Use C++ For this week’s lab you will write a program to read a data file containing numerical values, one per line. The program should compute average of the numbers and also find the smallest and the largest value in the file. You may assume that the data file will have EXACTLY 100 integer values in it. Process all the values out of the data file. Show the average, smallest, largest, and the name of the data file in the...

  • Write this program in C language Ensure the program output is exactly as described, and that...

    Write this program in C language Ensure the program output is exactly as described, and that the whitespace of your source code is well formatted. Utilise good naming practices when declaring variables. EXERCISE NAME: Recursive Movie In the history of Jurassic Park movies the following movies were released: Movie's Name: Jurassic Park The Lost World: Jurassic Park Jurassic Park III Jurassic World Year of Release: Rotten Tomatoes: 1993 93% 1997 51% 2001 50% 2015 72% Implement a structure type named...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT