Question
Compile and run the program first.Make sure that it can run.The program will calculate the sum of the series: sum(1/n^2)^i Currently the value, n, in
Compile and run the program first.Make sure that it can run.The program will calculate the sum of the series: sum(1/n^2)^i
Currently the value, n, in the series is set to 2.0 in the program.
Modify the program, so that the user can enter any value for n.
Modify the program, so that the user can enter the number of terms to use.
Modify the program, so that the value for n, the number of terms, and the sum of the series are all printed to the screen and to the output file: "output.txt".
Sample screen output (after you complete the modifications):
Run the program with n = 3 and 25 terms:
This program will calculate the sum of the series (1/n)^i.
What is your value of n?
3
How many terms do you want to calculate?
25
The sum of the series is = 1.125
Sample output.txt (after you complete the modifications):
For n = 3 and 25 terms, the series total is 1.125
MidtermMain.cpp:
#include
#include
#include
//function to raise a number to a power
//n is the power.
double funct(int i)
{
double value = 1; //this variable, value, holds the result.
// value is initialized to zero because x^0 = 1.
double factor = 1.0/(2.0*2.0);
//factor is the number that is raised to a power.
for (int j = 0; j
{
value = factor*value; //this will raise the factor to the power i
}
return value;
}
int main()
{
//This is a program to add the first terms of an infinite series.
//The series is the summation of (1/n^2)^i
std::ofstream fout("output.txt");
double sum=0; //max contains the largest potential factor to examine
for (int i = 0; i < 50; i++) //the first 50 terms of the series
{
sum = sum + funct(i);
}
std::cout << "The sum of the series is = " << sum << std::endl;
fout.close();
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started