Here is an infinite series to estimate the value of the cons

Here is an infinite series to estimate the value of the constant B.

B = 1/2 - 4/4 + 7/6 - 10/8 + ....

Assume a data file contains 25 integers. Each is greater than or equal to 1 and represents the number of terms that should be used to estimate B. Design a complete
C++ program that will read each of the integers, compute the estimated values of B, and display the number of terms used and estimate with 4 digits to the right
of the decimal. DO NOT USE FUNCTIONS. USE WHILE STATEMENTS, FOR LOOPS, AND/OR IF STATEMENTS.

Sample input (if the first 3 inputs are 5, 2, and 1)

Output:

Estimate of B using 5 term(s) is 0.7167
Estimate of B using 2 term(s) is -0.5000
Estimate of B using 1 term(s) is 0.5000

Here is part of my code, some of it might not be correct and it is incomplete. Please help me fill it in. If you can, please include comments on what variables are
for what, and what some of the code does.


#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
double num;
  cin >> num;
  cout << fixed << setprecision(4) << endl;

while (cin)
{

}

return 0;
}

Solution

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string name;
int count,hat;
double a,b,ans;
cout<<\"Enter the input file name: \";  
getline(cin,name);
ifstream myf;
myf.open(name.c_str());
cout << fixed << setprecision(4);
while(myf>>count)
{
        a = 1;
        b = 2;
        ans=0;
        hat = 1;
        for(int i=0;i<count;i++)
        {
            ans = ans + hat*a/b;
            a = a+3;
            b =b+2;
            hat = -1*hat;  
        }  
        cout<<\"Estimate of B using \"<<count<<\"term(s) is \"<<ans<<endl;
}
return 0;
}

Here is an infinite series to estimate the value of the constant B. B = 1/2 - 4/4 + 7/6 - 10/8 + .... Assume a data file contains 25 integers. Each is greater t
Here is an infinite series to estimate the value of the constant B. B = 1/2 - 4/4 + 7/6 - 10/8 + .... Assume a data file contains 25 integers. Each is greater t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site