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


Posted on 16th Feb 2014 07:03 pm by admin

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 #include 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 #include 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.

No comments posted yet

Your Answer:

Login to answer
58 Like 29 Dislike
Previous forums Next forums
Other forums

iMatch stored value with the current value in a loop
Hello,

I have a MySQL db were I store articles in.

I have a form to fill these article

download directory onto C drive
I am attempting (if this is possible) to write a routine to automatically dump the contents of a dir

extending tidy
I have problem with type-hinting and extending tidy. This code creates error:
Code: class cMyTidy

Displaying Multidimensional Arrays...about to put my head in a vice, PLEASE HELP
I am fairly new to PHP and MySQL and I have a problem which I have been trying to resolve for days.

help finding hacking loopholes
i was attacked by a redirect php injection

my pc is clean of viruses

so i figure that

Had a simple form script that suddenly stopped working
It was made about a year ago and had been working fine. Last time it was known to work for sure was

Struct/union and scope problem!
HI all , I have
Code: in header.h typedef struct Node Link; /* ---------

Get value from Select menu
Hi!

Have tried to find the solution on the web. Don't know if there is an easy one. A descrip

Preventing blank or already entered values
Im trying to learn how to place a read file line by line and prevent duplicate entries and also BLAN

my code does not work.
I have a code problem.

Quote"<a href=index.php?gogo=ureticidetay&'?l={$le

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash