Why will this program not run if the variables are not global?

Hey guys I have a kind of perplexing situation that is probably simple, but I can't seem to understand something. I have this code here that is basically a program that solves all 92 eight queens solutions, but prints it out in a 1D array of 8 numbers like an odometer. Each number tells you how many boxes down to go to put a queen inside (You go down the required number of boxes and then put a queen in the next one. EX: 47302516 - you put a queen in boxes 5,8,4,1,3,6,2,7 if you want to think about it from a non-programmer's perspective instead of starting from 0 because of the array going from 0 - 7). Now this program works perfectly fine, but I want to know why I have to declare the "int q[8]; int c;" variables as global for it to work. If I don't, the program just prints out nonsense.

Here is the working program:

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
#include <iostream>
#include <stdlib.h>
using namespace std;

//All the necessary functions

void nextRow(int[],int);
void backtrack(int[],int);
void print(int[],int);
bool ok(int[],int);

int q[8];   
int c;
                                                                                                          
int main()
{
        //This is the counter to effectively progress through the loop. the "flag" of the program.

        int counter;
              
        c = 0;   
        q[0] = 0;
        counter = 2;
        
        while(c > -1)
        {
                c++;
                counter--;
                if(c == 8){
                        print(q,c);
                        counter++; 
                }
                if(counter == 1){
                        q[c] = -1;
                        nextRow(q,c);
                }
        }
        return 0;
}

//This increments the row of the current column you are on, and if
the row is equal to 8, it knows to backtrack and try the next row in the
previous column.

void nextRow(int q[],int c)
{
        q[c]++;
        if(q[c] == 8)
                backtrack(q,c);
        ok(q,c);
}
                
//This is the test to see whether or not you can put a queen in that position.

bool ok(int q[],int c)
{
        for(int i = 0; i < c; i++)
        {
                if(q[c] == q[i] || abs(q[c] - q[i]) == abs(c - i))
                        nextRow(q,c);
        }
}

//Sets the current row equal to zero and goes back to the previous column
to increment the row.

void backtrack(int q[],int c)
{       
        q[c] = 0;
        c--;
        if(c == -1)
                exit(0);
        nextRow(q,c);
}

//Goes to the print function when c = 8.

void print(int q[],int c)
{
        for(int i = 0; i < 8; i++)
        {
                cout << q[i];
        }
        cout << endl;
        backtrack(q,c);
}



And here is the program that prints out nonsense:
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
#include <iostream>
#include <stdlib.h>
using namespace std;

//All the necessary functions

void nextRow(int[],int);
void backtrack(int[],int);
void print(int[],int);
bool ok(int[],int);

                                                                                                          
int main()
{
        //This is the counter to effectively progress through the loop. the "flag" of the program.

        int counter;
        int q[8];   
        int c;
        c = 0;   
        q[0] = 0;
        counter = 2;
        
        while(c > -1)
        {
                c++;
                counter--;
                if(c == 8){
                        print(q,c);
                        counter++; 
                }
                if(counter == 1){
                        q[c] = -1;
                        nextRow(q,c);
                }
        }
        return 0;
}

//This increments the row of the current column you are on, and if
the row is equal to 8, it knows to backtrack and try the next row in the
previous column.

void nextRow(int q[],int c)
{
        q[c]++;
        if(q[c] == 8)
                backtrack(q,c);
        ok(q,c);
}
                
//This is the test to see whether or not you can put a queen in that position.

bool ok(int q[],int c)
{
        for(int i = 0; i < c; i++)
        {
                if(q[c] == q[i] || abs(q[c] - q[i]) == abs(c - i))
                        nextRow(q,c);
        }
}

//Sets the current row equal to zero and goes back to the previous column
to increment the row.

void backtrack(int q[],int c)
{       
        q[c] = 0;
        c--;
        if(c == -1)
                exit(0);
        nextRow(q,c);
}

//Goes to the print function when c = 8.
void print(int q[],int c)
{
        for(int i = 0; i < 8; i++)
        {
                cout << q[i];
        }
        cout << endl;
        backtrack(q,c);
}


Any insight into this would be greatly appreciated for this aspiring C++ programmer :]. I want to know what I would have to change for it to work if the variables weren't global. I know it has to be something with the way I'm passing the variables through the functions, but I'm not sure. Thanks again guys.
These two programs work exactly the same way.
To find out why the printouts are different - you need to ask yourself this question:

What is the difference between a a local variable and a global variable - especially in regard to initialization??

Clue:
where the global variable prints out a 0 (zero) value, the local version shows a garbage value.


Now, I don't actually understand the problem you are trying to solve - so my question is as follows:
Is this loop supposed to run 2147483648 times?
1
2
3
4
5
6
7
8
9
10
11
12
13
while(c > -1)
        {
                c++;
                counter--;
                if(c == 8){
                        print(q,c);
                        counter++; 
                }
                if(counter == 1){
                        q[c] = -1;
                        nextRow(q,c);
                }
        }


If it is supposed to then OK.
Ah, yes, that is just set up so it keeps on running until c = -1. It finds that in the backtrack function.
Sorry I didn't have time to fully explain myself last time, I had to get to class, but what I am trying to solve, is how I would write the program without setting those variables as global. My professor told us to do it without using it, but I couldn't get it to work without setting those as global. I even manually initialized them to zero here:
1
2
3
4
5
6
7
int main()
{
        //This is the counter to effectively progress through the loop. the "flag" of the program.
        int counter;
        int c = 0;
        int q[8] = {0};
        q[0] = 0;


But it still gives me a garbage printout of one line. But when the int q[8] and int c, variables are global, it works perfectly. I want to know why this is and what I would have to change. Since I do not know what is going wrong.

The main reason for this is that now we have to write a program that will take "n-queens" and print out the total number of solutions in a 1D array. Meaning the user would enter in how many queens they want to put on the chessboard. Now since you can't have: "q[n]," we learned how to declare dynamic variables in the form of: int *q = new int[n];

However, I would not be able to set the array variable as global in this way, because it first requires the user to enter in a value for "n." This is why I need to figure out how to solve this problem without declaring the array and the column variable as global.
All I can say is that both programs give exactly the same output on my system (especially when q[8] is put in main function and initialised to 0) - that output
is one line that looks like this:

02000000

I just cannot see how you can be having two differnt sets of outputs (unless you have made modifications to the programs we don't know about - I'm using the programs you posted .)
Hmm, that's interesting. Perhaps it's because I'm using a different compiling program than you are, I'm using SSH at venus that my school uses. maybe I should try putting it in visual C++
Topic archived. No new replies allowed.