int main ()
{
int pi=2;
int pj=0;
int qi=2;
int qj=2;
int count=0;
int hold;
int num=50;
for(pi=0;pi<=num;pi++)
{
for(pj=0;pj<=num;pj++)
{
for(qi=0;qi<=num;qi++)
{
for(qj=0;qj<=num;qj++)
{
count+=isright(pi,pj,qi,qj);
cout<<pi<<" "<<pj<<" "<<qi<<" "<<qj<<" count = "<<count<<endl; //*****THIS LINE IS CAUSING THE PROBLEM*****
}
}
}
}
cout<<"\n\n\ncount is "<<count<<endl;
cout<<"\n\n\ncount is "<<count/2<<endl;
return 0;
}
As way of background, I just started programming a couple of weeks ago and I'm slowly working through the Euler Project challenges. I probably have some quite bad habits as I've never formally learned any programming. Generally my answers are through way of brute force methods.
What happens at line 50 when line 22 evaluates to true, but line 31, 37 and 43 evaluates to false?
In this case, your program's behaviour will become non-deterministic because variables a, b and c were not initialised. Line 50 may or may not evaluate to true because a, b and c contain garbage values from memory locations that can and probably was written to by the offending line 86.
Initialising a, b and c to a sane value OR catching the case where 22 is true, but 31, 37 and 43 is false will solve your problem.
If you use g++ with option -Wall when compiling, future occurences of this problem can be prevented.