#include %26lt;iostream.h%26gt;
using namespace std;
#include %26lt;math.h%26gt;
int main()
{
std::cout %26lt;%26lt; "The Zeta Function" %26lt;%26lt; std::endl;
int a, n, m, x, sum, total;
std::cout %26lt;%26lt; "Enter value for m, where m is the number of terms in the series that the series begins on" %26lt;%26lt; std::endl;
cin %26gt;%26gt; m;
std::cout %26lt;%26lt; "Enter value for n, where n is the number of terms in the series that the series ends on" %26lt;%26lt; std::endl;
cin %26gt;%26gt; n;
for (int x = m; x %26lt;= n; x++)
total = (1/(x^2));
std::cout %26lt;%26lt; "The sum of the series is " %26lt;%26lt; total %26lt;%26lt; "\n";
return 0;
}
The math function doesn't work and I want every term sumed up to that point to be printed then I want the total sum to be printed. The function is the Zeta Function
How do I do a summation series with in c++ and have it print out the terms?
Your code is very close but there are a few things wrong with it.
1. ^ is a bitwise operator XOR which can actually result in zero which means your code will try an do an illegal division by zero if you set m and n to be 2 and 3 for example.
You need use pow here. 1/x^2 = x^-2 so you can use
pow (x , -2).
2. All variables have been declared as integers which means any division 1/y = 0 for all y%26gt;1. So for all x^2 %26gt; 1 the division will lose all remainder and be zero.
You need to declare your variables as doubles for maximum precision.
double a, n, m, x, sum, total;
3. You are overwriting the value total each loop iteration and total is never initialised.
So you need to keep adding to total once it has been initialised to zero.
total = 0;
for (int x = m; x %26lt;= n; x++)
total += pow (x , -2);
4. User could type in values so m %26gt; n which means the loop terminates immediately leaving total as zero so we can swap these values if that's the case.
if( m%26gt; n) {
a=m;
m=n;
n=a;
}
Here is a corrected version of your code:
#include %26lt;iostream.h%26gt;
#include %26lt;math.h%26gt;
using namespace std;
int main()
{
std::cout %26lt;%26lt; "The Zeta Function" %26lt;%26lt; std::endl;
double a, n, m, x, sum, total;
std::cout %26lt;%26lt; "Enter value for m, where m is the number of terms in the series that the series begins on" %26lt;%26lt; std::endl;
cin %26gt;%26gt; m;
std::cout %26lt;%26lt; "Enter value for n, where n is the number of terms in the series that the series ends on" %26lt;%26lt; std::endl;
cin %26gt;%26gt; n;
if( m%26gt; n) {
a=m;
m=n;
n=a;
}
total = 0;
for (int x = m; x %26lt;= n; x++)
total += pow (x , -2);
std::cout %26lt;%26lt; "The sum of the series is " %26lt;%26lt; total %26lt;%26lt; "\n";
return 0;
}
Additional checks you might want are seeing if the user has input non-zero values and actual integers.
Reply:nstead of doing x^2 which is not supported, do x*x
Reply:^ is a bitwise XOR .. not the pow
you can use pow
double pow(double x, double y);
long double powl(long double x, long double y);
double pow10(int p);
long double pow10l(int p);
double pow10(int p);
long double pow10l(int p);
flower girl dresses
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment