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<

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
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|

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

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY