### Coding Help (C++)

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{

double i;
for( i = 1; i<=10; i++)
{
if( i == 1 ) cout << "1";
if( i % 2.0 == 0.0) cout << " - 1/" << 2 * i - 1;
if( i % 2.0 != 0.0) cout << " + 1/" << 2 * i - 1;
}

return 0;
}

Every time I try to compile it, I keep getting an error. Before you ask why I assigned i as a double, this is just part of a bigger project I'm working on, but the main issue is located within the if statements. Any advice?
Last edited on
There is no such operator % for double numbers. So the compiler shall issue an error.
So how would I go about finding if i is even or odd?
Take a look here. http://bytes.com/topic/c/answers/495889-modulus-double-variables

floats and doubles don't have remainders. I completely forgot about that.
I guess it would help if I pasted the entire code.

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{

int n;
double sum = 0, pi, i;

cout << "This program approximates pi using an n-term series expansion." << endl;
cout << "Enter the value of n> ";
cin >> n;

if (n <= 0) cout << n << " is an invalid number of terms." << endl;

else
{
cout << "pi["<<n<<"] = 4[";

for(i = 1; i<=n; i++)
{
sum = sum + (pow(-1,i + 1) * (4 / (2 * i - 1)));

if( i == 1 ) cout << "1";
if( i % 2.0 == 0.0) cout << " - 1/" << 2 * i - 1;
if( i % 2.0 != 0.0) cout << " + 1/" << 2 * i - 1;

}
cout << "] = "<< setprecision (20) << sum << endl;
return 0;
}
The problem is because of the doubles. Modulus doesn't work with them. Try it this way..

 ``123456789101112131415161718192021`` ``````#include #include #include using namespace std; int main() { double i; for( i = 1; i<=10; i++) { if( i == 1 ) cout << "1"; if( i % 2 == 0.0) cout << " - 1/" << 2 * i - 1; if( i % 2 != 0.0) cout << " + 1/" << 2 * i - 1; } return 0; }``````
 @whitenite1

One more floating numbers have no operator %.
Last edited on
The problem comes in with pow(-1,i+1). Since it only works with i being a double, its throwing off my if statements. I could always make another variable and have it be positve or negative one depending on the value of i, but that seems very inefficient.
Well, before you ask if a double is even or odd, I guess you've got to ask if it's a whole number?

Andy

PS Any mathematicians about? Can a non-integer be odd or even? I guess 3.1415... isn't divisible by two, so it can't be even. But is it odd??
Last edited on
It should always be an integer, because i starts at one and is incremented by 1. I was trying to work around my pow(-1,i + 1), but it looks like i'll have to change that.
How about this fix?

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{

int n, i;
double sum = 0, pi;

cout << "This program approximates pi using an n-term series expansion." << endl;
cout << "Enter the value of n> ";
cin >> n;

if (n <= 0) cout << n << " is an invalid number of terms." << endl;

else
{
cout << "pi["<<n<<"] = 4[";

for(i = 1; i<=n; i++)
{
int pow;
if (i % 2 != 0) pow = 1;
if (i % 2 == 0) pow = -1;

sum = sum + (pow * (4 / (2 * i - 1)));

if( i == 1 ) cout << "1";
if( i % 2 == 0) cout << " - 1/" << 2 * i - 1;
if( i % 2 != 0) cout << " + 1/" << 2 * i - 1;

}
cout << "] = "<< setprecision (20) << sum << endl;
return 0;
}
}
Last edited on
I think you might need to use

`sum = sum + (pow * (4.0 / (2.0 * i - 1.0)));`

the presence of the double literals will force double arithmatic to be used. Otherwise it might use integer division and multiplication and only promote the result of that for the addition. (I'm a little unsure of the promotion rules at this time of the evening...)

If the literals weren't there, you'd need to cast one or more of the ints involved in the calculation to force promotion.

Andy

PS I think one double literal would be enough, but I think it looks nicer (!) when all are the same.
Last edited on
I would like to thank everyone for brainstorming and coming up with solutions to my coding problems. *high five*
Topic archived. No new replies allowed.