The <cmath> function sqrt(x) is used to calculate the exact square-root value of
ID: 3808608 • Letter: T
Question
The <cmath> function sqrt(x) is used to calculate the exact square-root value of the number x such that x is a non-negative real number. The square root value of x can also be estimated using the following mathematical equation:
Where e = 2.718282, and ln is the natural logarithm; called log() in <cmath>.
Write a C++ program that calculates and writes to an output file the exact and estimated square roots of a list of numbers. The list of numbers is either read from the keyboard, generated randomly or read from an input file. The program displays to the user the following menu and performs the corresponding tasks based on the user’s selection. The program should keep running until the user enters ‘Q’ or ‘q’.
K: Read the list of numbers from the keyboard R: Generate the list of numbers randomly
F: Read the list of numbers from an input file Q: Quit the program
If the user enters ‘K’ or ‘k’, the program should read a list of numbers terminated by Ctrl+z from the keyboard.
If the user enters ‘R’ or ‘r’, the program should ask the user to specify how many numbers to generate randomly and then generates that many random values. The generated numbers MUST range between 5.000 and 20.000. For this purpose use the rand() function to return values between 5000 and 20000 and divide them by 1000.
If the user enters ‘F’ or ‘f’, the program should read the list of numbers from an input file which could be empty or contain any number of values given individually on separate lines. The name of the input file is provided by the user at run-time.
For each of the 3 menu options K, R and F, the program should calculate the exact and estimated square roots of the obtained list of numbers and write the results in an output file called ‘square_roots.txt’. The program should ignore (skip) any negative values in the list of numbers and do the calculations and display the results only for the non-negative values.
The output should be written to the output file in tabular format including, for each non-negative number x in the list, the number x itself, its exact root, its estimated root, and the absolute value of the difference between the two roots. Format the x’s with 3 digits after the decimal point and format the roots and the difference between them with 6 digits after the decimal point.
ln(r Vr = e2Explanation / Answer
#include<iostream>
#include<cmath>
#include<conio>
#include<cstdlib>
#include<iomanip>
#include<fstream>
using namespace std;// used to resolve namespace conflicts
void main()
{
drouble x;//gives double precision as specified in question
char inputkeyboard;// different types of char input from keyboard
int count = 0;// used as a counter to keep the count of values required to be used
ofstream fout;// output for files processing
ifstream fin;// input for file processing
clrscr();
do// using loop statements for considering input values
{
cout<<"enter the input value ";
cin>>inputkeyboard;
fout.open("square_roots.txt");
fout<<fixed<<showpoint;
if(fout.fail())
{
cout<<"unable to open square_roots.txt file";
return 1;
}
switch(inputkeyboard)
{
case 'k':
case 'K':
cout<<"display the values entered from keyboard untill terminated by crtl+z";
cout<<"Enter the value of x";
cin>>x;
while(!cin.eof())
{
if(x>0)
{
fout<<setprecision(3)<<"x="<<x<<setprecision(6)<<"x="<<x<<sqrt(x)<<estimated roots are as<<exp(1/2)*log(x));
fout<<"difference between two numbers can be stimated by using the absolute function "<<abs(sqrt(x)-exp((1/2)*log(x)))<<endl;
count++;
cin>>x;
break;
case 'r':
case 'R':
cout<<"Generate list of numbers randomly";
fin.open("numbers.txt");
if(fin.fail())
{
cout<<"unable to open up the file";
return 1;
}
fin>>x;
while(x>0)
{
fout<<setprecision(3)<<"x="<<x<<setprecision(6)<<"x="<<x<<sqrt(x)<<estimated roots are as<<exp(1/2)*log(x));
fout<<"difference between two numbers can be stimated by using the absolute function "<<abs(sqrt(x)-exp((1/2)*log(x)))<<endl;
count++;
fin>>x;
}
fin.close();
break;
case 'f':
case 'F':
cout<<"read the data from input file provided by user at run time";
cout<<"Generate list of numbers randomly";
fin.open("numbers.txt");
if(fin.fail())
{
cout<<"unable to open up the file";
return 1;
}
fin>>x;
while(x>0)
{
fout<<setprecision(3)<<"x="<<x<<setprecision(6)<<"x="<<x<<sqrt(x)<<estimated roots are as<<exp(1/2)*log(x));
fout<<"difference between two numbers can be stimated by using the absolute function "<<abs(sqrt(x)-exp((1/2)*log(x)))<<endl;
count++;
fin>>x;
}
fin.close();
break;
case 'q':
case 'Q':
cout<<"Quit the program";
cin>>x;
break;
default:
cout<<"Invalid case";
break;
}
fout.close();
while( inputkeyboard != 'q' && inputkeyboard != 'Q' )
{
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.