- #1
fluidistic
Gold Member
- 3,923
- 261
Hello! As a new past time I wanted to learn some coding. A friend of mine wrote a program and explained it from A to Z to me, it took like 2 hours.
I wanted to play a bit with the program by modifying it.
Here's the code:
Basically what the program does is pick a random MxN matrix element, check whether that element was already picked, in case of yes, go back and repeat the process to pick a random matrix element. If the matrix element was not picked before, check out whether the adjacent entries were already picked. If not, repeat. If there's an adjacent entry stop and return "placed", which corresponds to the number of chosen matrix elements.
Repeat the process "TRIES" times. Finally, report the average "placed" value we get after "TRIES" tries.
For example if you pick N=M=8 then the program will tell you how many pieces a program that places chess pieces randomly over a chess board will have to put in average so that 2 pieces are adjacent. For N=M=19 you have the answer for the 19x19 goban (go board).
Now for fun I wanted to see whether the relation for a square board size (or square matrix in the program) would be linear with N (turns out that it is! Or almost...).
So I wanted to create a new loop in the program that would execute it for N=M=j where j goes from 2 to 100.
I have tried several things, all failed. Also the way N and M are defined, I am not sure I can change their values (they would change if I assignate their value to j).
The following code:
returns lots of errors during the compile:
Google told me that's because I defined a loop outside the main function.
So I tried to define the loop inside the main function. The problem is that it does not know what N stands for when I do so, because the function "tries()" is written before "main()"...
Here's another attempt. I first define N to be 1 before both functions are written. And then in main() I try the loop. And I get the error that N is "read only". Details:
returns
Also, I cannot seem to compile when I change "static const int N" for "int N", and I don't understand why. Here is what I got (from the very last code I posted here) when I try so:
I wanted to play a bit with the program by modifying it.
Here's the code:
Code:
#include <stdlib.h>
#include <iostream>
#include <time.h>
using namespace std;
static const int M = 32;
static const int N = 54;
static const int TRIES = 1000000;
int tries() {
bool board[N + 2][M + 2] = {};
for (int placed = 0;;) {
// Pick an x,y position on the board...
int x = rand() % N + 1; // so the N is for the x-coordinate
int y = rand() % M + 1; // M for y
// Is there already a stone at this position?
if (board[x][y])
// Ignore it and go pick another position
continue;
if (board[x][y - 1] || board[x - 1][y] || board[x + 1][y] || board[x][y + 1])
// Return the number of stones that have been placed
return placed;
// Place a stone at this position
board[x][y] = true;
// Keep track of the number of stones that have been placed
placed++;
}
}
int main() {
srand(time(0));
int placed = 0;
for (int t = 0; t < TRIES; t++)
placed += tries();
float average = placed / (float)TRIES;
cout << "It took an average of " << average << " tries" << endl;
}
Repeat the process "TRIES" times. Finally, report the average "placed" value we get after "TRIES" tries.
For example if you pick N=M=8 then the program will tell you how many pieces a program that places chess pieces randomly over a chess board will have to put in average so that 2 pieces are adjacent. For N=M=19 you have the answer for the 19x19 goban (go board).
Now for fun I wanted to see whether the relation for a square board size (or square matrix in the program) would be linear with N (turns out that it is! Or almost...).
So I wanted to create a new loop in the program that would execute it for N=M=j where j goes from 2 to 100.
I have tried several things, all failed. Also the way N and M are defined, I am not sure I can change their values (they would change if I assignate their value to j).
The following code:
Code:
#include <stdlib.h>
#include <iostream>
#include <time.h>
using namespace std;
for (int j=2;j<101;j++)
static const int N = j;
static const int TRIES = 100000;
int tries() {
bool board[N + 2][N + 2] = {};
for (int placed = 0;;) {
int x = rand() % N + 1;
int y = rand() % N + 1;
if (board[x][y])
continue;
if (board[x][y - 1] || board[x - 1][y] || board[x + 1][y] || board[x][y + 1])
return placed;
board[x][y] = true;
placed++;
}
}
int main() {
srand(time(0));
int placed = 0;
for (int t = 0; t < TRIES; t++)
placed += tries();
float average = placed / (float)TRIES;
cout << "It took an average of " << average << " tries" << endl;
}
Code:
PFadjacency.cpp:7:1: error: expected unqualified-id before ‘for’
for (int j=2;j<101;j++)
^
PFadjacency.cpp:7:14: error: ‘j’ does not name a type
for (int j=2;j<101;j++)
^
PFadjacency.cpp:7:20: error: ‘j’ does not name a type
for (int j=2;j<101;j++)
^
PFadjacency.cpp: In function ‘int tries()’:
PFadjacency.cpp:14:16: error: ‘N’ was not declared in this scope
bool board[N + 2][N + 2] = {};
^
PFadjacency.cpp:21:13: error: ‘board’ was not declared in this scope
if (board[x][y])
^
PFadjacency.cpp:24:13: error: ‘board’ was not declared in this scope
if (board[x][y - 1] || board[x - 1][y] || board[x + 1][y] || board[x][y + 1])
^
PFadjacency.cpp:28:9: error: ‘board’ was not declared in this scope
board[x][y] = true;
^
So I tried to define the loop inside the main function. The problem is that it does not know what N stands for when I do so, because the function "tries()" is written before "main()"...
Here's another attempt. I first define N to be 1 before both functions are written. And then in main() I try the loop. And I get the error that N is "read only". Details:
Code:
#include <stdlib.h>
#include <iostream>
#include <time.h>
using namespace std;
static const int N = 1;
static const int TRIES = 100000;
int tries() {
bool board[N + 2][N + 2] = {};
for (int placed = 0;;) {
int x = rand() % N + 1;
int y = rand() % N + 1;
if (board[x][y])
continue;
if (board[x][y - 1] || board[x - 1][y] || board[x + 1][y] || board[x][y + 1])
return placed;
board[x][y] = true;
placed++;
}
}
int main() {
for (int j=2;j<101;j++) {
N=j;
srand(time(0));
int placed = 0;
for (int t = 0; t < TRIES; t++)
placed += tries();
float average = placed / (float)TRIES;
cout << "It took an average of " << average << " tries" << endl;
}
}
Code:
PFadjacency.cpp: In function ‘int main()’:
PFadjacency.cpp:37:3: error: assignment of read-only variable ‘N’
N=j;
^
Also, I cannot seem to compile when I change "static const int N" for "int N", and I don't understand why. Here is what I got (from the very last code I posted here) when I try so:
Code:
PFadjacency.cpp: In function ‘int tries()’:
PFadjacency.cpp:14:33: error: variable-sized object ‘board’ may not be initialized
bool board[N + 2][N + 2] = {};
^