I need this program to work on Ideone.com in C++ The Program complies right but
ID: 3530138 • Letter: I
Question
I need this program to work on Ideone.com in C++ The Program complies right but the out put is like repeating terms. I'm trying to get this: Sample run 1: This program approximates e^x using a n-term power series expansion. Enter the value of x>2 How many terms do you wish to use in this approximation?-3 -3 is an invalid number of terms. AND Sample Run 2: This program approximates e^x using a n-term power series expansion. Enter the value of x>1 How many terms do you wish to use in this approximation?5 e^1 = 1 + 1 + 0.5 + 0.166667 + 0.0416667 = 2.70833333333333303727 The exact value of e^1.00000000000000000000 is 2.71828182845904509080. Approximation error is 0.00994849512571205352. This is my code that I am working on and put into ideone.com in C++ but doesn't work correctly: #include#include #include using namespace std; int main() { int i,j; double x,n,total,fact,term,exact,val; fact = 1; cout<<"This program approximates e^x using a n-term power series expansion "; cout<<"Enter the value of x: "; cin>>x; cout<<"How many terms do you wish to use in this approximation: "; cin>>n; while(n<0) { cout<<n<<" is an invalid number of terms. "; cout<<"How many terms do you wish to use in this approximation: "; cin>>n; } cout<<" e^"<<x<<" = "; total=1; cout<<total; for(i=1;i<=n;i++) { fact*=i; val=(pow(x,(double)i)); term=val/fact; cout<<" + ("<<val<<"/"<<fact<<")"; total+=term; } cout<<" = "<<setprecision(20)<<fixed<<total<<endl; exact=exp(x); cout<<"Exact value of e^"<<x<<" is "<<setprecision(20)<<fixed<<exact<<endl; cout<<"Approximation error is "<<setprecision(20)<<fixed <<exact-total<<". "; // system("pause"); return 0; }Explanation / Answer
This code works ,checked in ideone Please rate it first
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.