Write a program in C++ to ask the user to input two different numbers of type do
ID: 674684 • Letter: W
Question
Write a program in C++ to ask the user to input two different numbers of type double called actual and estimated. The program should do the following: Check if estimated is different from actual. If estimated is not different from actual, the user will be prompted to enter a new value Calculate and display the absolute error between the actual and estimated values Calculate and display the relative error between the actual and estimated values Determine and display the accuracy of the approximation obtained (the number of significant digits)Explanation / Answer
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
double actual, estimated;
double relativeError,absoluteError=0;
string sig,s;
cout<<endl<<"Enter the Actual number: ";
cin>>actual;
while(absoluteError==0)
{
cout<<endl<<"Enter the Estimated number: ";
cin>>estimated;
absoluteError=actual-estimated;
if(absoluteError==0)
{
cout<<endl<<"Estimated number should not be same as actual. ";
cout<<endl<<"Re enter: ";
}
}
relativeError = absoluteError / estimated;
cout<<endl<<"The absolute error is "<<absoluteError;
cout<<endl<<"The relative error is "<<relativeError;
std::ostringstream os;
os <<relativeError ;
sig = os.str();
sig=sig.substr(sig.find("."))+1;
cout<<" number of significant digits: " <<sig.length();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.