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| Wrong test - this won't work if r is very near one. Try a=2, r=.99999 With smaller values of r, it is asking for too much precision. With a=2, r=.99, it is getting 10 significant digits. HELP ME FIX CODE. PLEASE USE MY CODE FIX #include #include using namespace std; int main() { //declare variables int terms; double a, r, number, sumbyformula, sum2; //take user input of a cout<<"Enter a value of first term a: "; cin>>a; //take user input of r cout<<"Enter a value of ratio r: "; cin>>r; //validate the ratio r, untill valid value entered while(r>=1 or r<=-1) { //print meassage and take input again cout<< "Invalid! Please enter a value between -1 and 1 for ratio: "; cin>>r; } //compute the sum by the formula sumbyformula=a/(1-r); //set number to a number=a; //initialise the sum2 and terms to 0 sum2=0; terms=0; //check the condition for diffence between both sum while(fabs(sumbyformula-sum2) >= fabs(a*0.0000001)) { //compute the sum by adding the number sum2=sum2+number; //multiply r to the number number=number*r; //increase terms terms+=1; } cout<
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|
Wrong test - this won't work if r is very near one. Try a=2, r=.99999
With smaller values of r, it is asking for too much precision. With a=2,
r=.99, it is getting 10 significant digits.
HELP ME FIX CODE. PLEASE USE MY CODE FIX
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
//declare variables
int terms;
double a, r, number, sumbyformula, sum2;
//take user input of a
cout<<"Enter a value of first term a: ";
cin>>a;
//take user input of r
cout<<"Enter a value of ratio r: ";
cin>>r;
//validate the ratio r, untill valid value entered
while(r>=1 or r<=-1)
{
//print meassage and take input again
cout<< "Invalid! Please enter a value between -1 and 1 for ratio: ";
cin>>r;
}
//compute the sum by the formula
sumbyformula=a/(1-r);
//set number to a
number=a;
//initialise the sum2 and terms to 0
sum2=0;
terms=0;
//check the condition for diffence between both sum
while(fabs(sumbyformula-sum2) >= fabs(a*0.0000001))
{
//compute the sum by adding the number
sum2=sum2+number;
//multiply r to the number
number=number*r;
//increase terms
terms+=1;
}
cout<<endl;
//Set decimal place
cout.precision(10);
//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;
//print the number of terms added
cout<<"The number of terms added are: "<<terms<<endl;
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images