Okay, so I'm trying to compute an infinite sum using a for loop instead of a while loop, and the darn code isn't working and I'm not sure why. I get the two "I got here"'s ONCE and then nothing. :\
Does anyone know why my code isn't working and just looks like it freezes or hangs? The loops should end with the way I made them, right? Here's the current problem code
check=0;
cerr << "I got here" << endl;
for (int m=1;m<m+1;m++)
{
cerr << "I got here" << endl;
for (int n=1;n<n+1;n++)
{
if (check==0)
{
anaPhi[i][j][k]=1.0/(m*m*m*n*n*n)
*cos(k*deltat*sqrt(5.0)*pi/4.0
*sqrt(m*m+4.0*n*n))
*sin(m*pi*i*h/4.0)
*sin(n*pi*j*h/2.0);
check=1;
}
else
{
anaPhi[i][j][k]= anaPhi[i][j][k]
+1.0/(m*m*m*n*n*n)
*cos(k*deltat*sqrt(5.0)*pi/4.0
*sqrt(m*m+4.0*n*n))
*sin(m*pi*i*h/4.0)
*sin(n*pi*j*h/2.0);
}
}
}
}
I just tested this code and this code also doesn't work!
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int b=0;
for (int m=1;m<m+1;m=m+2)
{
b=cos(b)+sqrt(b);
}
cout << b << endl;
return 0;
}
I see a couple of issues.
if(check=0)
will always set check to 0.
Because you use n= n+2
, it will not ever break. Because at some point you get to n = 0x7FFFFFFE (highest positive even number) and in this case n+1 is > n. It then wraps to 0x80000000 (the most negative signed int, still even) and n+1 will still be > n. If you were able to get n = 0x7FFFFFFF, then n + 1 would actually be less than n due to integer wrap around.
There does not seem to be any other way to break out of the for loops than meeting the coditions set.