Programming Error

Hey guys, I have a huge question. I have to write a program that uses negative numbers in a conjecture algorithm. The program terminates when a number is reached that was already outputted. I can not get it to work and I have been racking my brain for a week. Any help would be wonderful Thanks!

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

#include <iostream>

using namespace std;

int main()
{
    int x[1000], y[1000], i=0, j=0, count=0, w=0, z[1000], t = 0;
    
    for (i=0; i<1000; i++) {
        x[i]=(i+1)*-1;
        
        z[t]=x[i];
        
        //cout<<x[i]<<"      "<<z[t]<<endl;
        
        t++;
        
    }
    
   //all y's = 0
    for (j=0; j<1000; j++) {
        y[j]=0;
    }
    
    while (w!=1) {
        
    if (x[i]%2==0) {
        y[j]=x[i];
        x[i]=x[i]/2;
    }
    
    else {
        y[j]=x[i];
        x[i]=(3*x[i]+1)/2;
    }
    
    for (j=0; j<1000; j++) {
        if (x[i]==y[j]) {
            w=1;
        }
        
        else{
            y[j]=x[i];
            count++;
            j++;
        }
    }
    
        cout<<z[t]<<"      "<<count<<endl;
        t++;
    
        
    }//end while (w!=1) loop

    
    
    return 0;
}
Have you run it in a debugger, to see what's happening? Or even put in any debugging output?

From looking at it, once your first loop exits at line 19, the value of i will be set to 1000, and never changes. Every time you access x[i], you're accessing x[1000], which is past the end of the array and could contain anything.
AH HA! that makes sense; I have a mac and never used a debugger. Any thoughts on what should be used?
Topic archived. No new replies allowed.