Coding Help (C++)

Oct 16, 2012 at 7:13pm
#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 Oct 16, 2012 at 7:18pm
Oct 16, 2012 at 7:20pm
There is no such operator % for double numbers. So the compiler shall issue an error.
Oct 16, 2012 at 7:22pm
So how would I go about finding if i is even or odd?
Oct 16, 2012 at 7:22pm
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.
Oct 16, 2012 at 7:24pm
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;
}
Oct 16, 2012 at 7:25pm
The problem is because of the doubles. Modulus doesn't work with them. Try it this way..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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)
			cout << " - 1/" << 2 * i - 1;
		if( i % 2 != 0.0)
			cout << " + 1/" << 2 * i - 1;
	}
	return 0;
}
Oct 16, 2012 at 7:28pm
@whitenite1


One more floating numbers have no operator %.
Last edited on Oct 16, 2012 at 7:28pm
Oct 16, 2012 at 7:29pm
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.
Oct 16, 2012 at 7:29pm
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 Oct 16, 2012 at 7:31pm
Oct 16, 2012 at 7:32pm
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.
Oct 16, 2012 at 7:35pm
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 Oct 16, 2012 at 7:36pm
Oct 16, 2012 at 7:50pm
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 Oct 16, 2012 at 7:53pm
Oct 16, 2012 at 8:23pm
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.