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|

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

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;
}

finclude <iostream>
finclude <cmath>
using namespace std;
int main ()
int i = 1;
double a, b, I, 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<l
while (r>-1 and r<=-1)
Doesn't work - try a = 2, 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))
Don't use pow; find the
sum2=sum2+number;//compute the sum by add the number current term by multiplying
the previous term by r.
number-a" (pow (r, b) );
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;
Transcribed Image Text:finclude <iostream> finclude <cmath> using namespace std; int main () int i = 1; double a, b, I, 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<l while (r>-1 and r<=-1) Doesn't work - try a = 2, 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)) Don't use pow; find the sum2=sum2+number;//compute the sum by add the number current term by multiplying the previous term by r. number-a" (pow (r, b) ); 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;
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education