Commenting out cout (print) code causes different result

Hi

I have written a code (posted below) and when I comment out this line:

cout<<pi<<" "<<pj<<" "<<qi<<" "<<qj<<" count = "<<count<<endl;

I get a different result.

The result when not commented is count = 28 (which is correct).
The result when commented is count = 33 (which is incorrect).

Does anyone know why this would be happening?

I am not changing anything else in the code and cannot see why this would happen.

Note: 'isright' is my own function.

Thanks for any help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

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;
}



Odd behavior like that hints of heap corruption. The problem is probably in the isright function (or one of the functions it calls).

Can you post the whole program or is it too large?
The whole program is below.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <sstream>
#include <string>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <math.h>

using namespace std;

int isright(int pi,int pj,int qi, int qj)
{
//    int pi,pj,qi,qj;
    int lpa2,lpq2,lqa2;
    int a,b,c;
    int tri;

//    pi=3;
//    pj=1;
//    qi=3;
//    qj=2;

    if(!((pi==qi)&&(pj==qj)))
    {
        lpa2=pi*pi + pj*pj;
        lqa2=qi*qi + qj*qj;
        lpq2=(pi-qi)*(pi-qi) + (pj-qj)*(pj-qj);

//        cout<<(lpa2)<<" "<<(lqa2)<<" "<<(lpq2)<<" "<<endl;
        // Find the biggest
        
        if((lpa2>lqa2)&&(lpa2>lpq2))
        {
            a=lpa2;
            b=lqa2;
            c=lpq2;
        }
        if((lqa2>lpa2)&&(lqa2>lpq2))
        {
            a=lqa2;
            b=lpa2;
            c=lpq2;
        }
        if((lpq2>lpa2)&&(lpq2>lqa2))
        {
            a=lpq2;
            b=lpa2;
            c=lqa2;
        }
//        cout<<a<<"  "<<b<<"  "<<c<<endl;
        if(a==(b+c))
        {
//            cout<<"right"<<endl;
            return(1);
        }
        else
        {
//            cout<<"not right"<<endl;
            return(0);
        }
    }
    else
    {
        return(0);
    }
}

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;
//                    cin>>hold;
                }
            }
        }
    }

    cout<<"\n\n\ncount is "<<count<<endl;

    cout<<"\n\n\ncount is "<<count/2<<endl;
    return 0;
}

Hi,

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.
Last edited on
Topic archived. No new replies allowed.