Homework write a program that will add the terms of an infinite geometric series. The program should read the first term (a) and the common ratio (r) for the series. It should the compute the sum in two ways: by formula (s=a/1-r), and by adding the individual terms until the answer agrees with the formula to 7 significant digits. The print the formula answer, the answer found by adding terms, and the number of terms that were added. Print the sums to at least ten places. Verify input with a while loop. Two real values are equal to n significant digits if the following condition is true: |a-b|<|a*10^-n|
Homework write a program that will add the terms of an infinite geometric series. The program should read the first term (a) and the common ratio (r) for the series. It should the compute the sum in two ways: by formula (s=a/1-r), and by adding the individual terms until the answer agrees with the formula to 7 significant digits. The print the formula answer, the answer found by adding terms, and the number of terms that were added. Print the sums to at least ten places. Verify input with a while loop. Two real values are equal to n significant digits if the following condition is true: |a-b|<|a*10^-n|
HELP ME FIX MY CODE comment in the picture.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int i = 1;
double a, b, r, number, sumbyformula, sum2;
cout<<"Enter a value of first term a: ";
cin>>a;
cout<<"Enter a value of ratio r: ";
cin>>r;
// Loop -1<r<1
while(r>=1 and r<=-1)
{
cout<< "Enter the number between -1 and 1 for the series\n";
cin>>r;
}
//compute the sum by the formula
sumbyformula = a/(1-r);
//check the condition for both of sum
while(fabs(sumbyformula-sum2) < fabs(a*0.0000001))
{
number=a*(pow(r,b));
sum2=sum2+number;//compute the sum by add the number
i++;
}
cout<<endl;
//Set decimal place
cout.precision(15);
//Set scientific notation and print formula S= a/(1-r)
cout<<scientific<<"Sum by formula S= a/(1-r) is "<<sumbyformula<<endl;
//Set scientific notation and print the series numbers
cout<<scientific<<"Sum by add the series numbers is "<<sum2<<endl;
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps