1. ## Trouble with matrices

I'm pretty new to programming, so there is probably some obvious mistake that I am missing, but here goes. I am trying to create random rooms on the screen that a character can explore in a very simple dungeon-esque game. However, when I assign a value to a certain point on the matrix, it assigns that value to the entire column. Here is the code:
Code:
```getmaxyx(stdscr, row, col);
int room1[row][col];
for(d = 0; d <= row; d++) {
for(e = 0; e <= col; e++) {
room1[d][e] = 0;
}
}
for(f = 1; f <= 10; f++) {

srand(time(NULL)*seed1);
g = rand() % (row);
seed2 = rand()/(RAND_MAX * 1.0);
srand(time(NULL)*seed2);
h = rand() % (20);
h = g + h;
seed3 = rand()/(RAND_MAX * 1.0);
srand(time(NULL)*seed3);
i = rand() % (col);
seed4 = rand()/(RAND_MAX * 1.0);
srand(time(NULL)*seed4);
j = rand() % (20);
j = i + j;
seed1 = rand()/(RAND_MAX * 1.0);
mvprintw(0,0,"(%d, %d)",g,h);
getch();
for(o = g; o <= h; o++) {
for(p = i; p <= j; p ++) {
room1[o][p] = 1;       /* Here is the problem */
}
}
}
for(m = 0; m <= row; m++) {
for(n = 0; n <= col; n++) {
if(room1[m][n] == 1) {
mvprintw(m,n, "1");
}
}
}```

2. I'm having a very difficult time understanding this code. You should give your variables more descriptive names; it's difficult to follow the code.

Also, you only need to call "srand()" once, at the beginning of your program.

You'd get more help if you provided the simplest amount of code that can be compiled and that illustrates the problem. As it stands, we can only look at what you've coded and, as I've mentioned, it is not clear. Even a few comments between lines of code would be a big help.

And ... did you actually get this to compile? Because I see big potential problems with this code.

For instance:

- 'g' is given a random value between zero and "row"
- 'h' is given a random value between zero and nineteen
- 'h' is updated to the sum of itself and 'g'
- In the "for" loop, 'o' starts out equal to 'g' and gets incremented to 'h'

So let's assume:
- "row" is 30
- 'g' is 21
- 'h' is 16

-> therefore: h = 16+21 = 37

In "room1[o][p]" the variable 'o' can at most be "row" minus one ("room1[29][p]").

You'd be trying to access "room1[37][p]" which is way out of bounds of the array.

In summary, you need to rethink the logic of this code.

3. Usually, srand is called once at the beginning of the program. Calling it again and again doesn't make it more random. Here's your program with all the srand stuff taken out and the variable names rationalized.
Code:
```getmaxyx(stdscr, &ysize, &xsize);
int room1[ysize][xsize];

for(y = 0; y < ysize; y++)
for(x = 0; x < xsize; x++)
room1[y][x] = 0;

for(i = 0; i < 10; i++) {
y1 = rand() % ysize;
y2 = y1 + rand() % 20;
x1 = rand() % xsize;
x2 = x1 + rand() % 20;
mvprintw(0, 0, "(%d, %d)", y1, y2);
for(y = y1; y < y2; y++)
for(x = x1; x < x2; x++)
room1[y][x] = 1;
}

for(y = 0; y < ysize; y++)
for(x = 0; x < xsize; x++)
if(room1[y][x] == 1)
mvprintw(y, x, "1");```
The code seemingly writes random squares of 1's to room1.
Can't y2 and x2 be out of range?

4. Thank you so much for your input. It really helped a lot. However, I still have the same problem. Referring to the program you posted, the value assignment in line 16 still is assigning all of the points on the array with that x value (in other words the entire column) to 1, not just that one point. When I run this program, I always end up with entire columns of ones. Is this just a glitch, or is something else probably wrong?

5. Originally Posted by EmJayDeeZee
Referring to the program you posted, the value assignment in line 16 still is assigning all of the points on the array with that x value (in other words the entire column) to 1, not just that one point.
What point do you want to assign 1?
If you want to set just one element of the array to 1, you simply do something like
Code:
`room1[4][5] = 1    // sets the element in the 5th row, 6th column to 1`
If you want a random element do
Code:
```y = rand() % ysize;
x = rand() % xsize;
room1[y][x] = 1;```
You don't need for-loops for setting just one element.

Bye, Andreas