# Generate board

• 03-07-2010
oleg174
Generate board
I need to create a program that receives two numbers from the user (rows and columns) and then generates a board with arrows in random direction. In the middle of the board should be a diamond.

This is my code. It does not generate arrows and does not create diamond in the middle.

Code:

``` #include <cstdlib> #include <iostream> #include <math.h> #include <stdlib.h> #include <time.h> #include <stdio.h> /* Function that generates the board */ int boardgame(int row, int column) { int i,j, board2[row][column],num; float r,c; srand(time(NULL)); r=row; c=column; for (i=0; i<row; i++) {     for (j=0; j<column; j++)     {         if ((j==(c/2.0)) && (i==(r/2.0))) {         board2[i][j]=35; }       else if (((j-1)==(c/2.0)) && (i==(r/2.0))) {         board2[i][j]=59; }       else if ((j==(c/2.0)) && ((i-1)==(r/2.0))) {         board2[i][j]=59; }       else if (((j-1)==(c/2.0)) && ((i-1)==(r/2.0))) {         board2[i][j]=35; }         else {         num=(10 + rand()% 4);         board2[i][j]=num;         }         }         }         return (board2[row][column]);         } int main() {     int z,x,row,column, q1=1;     int board[20][20];         /* Checks if the entered # of rows and columns is correct */     while (q1==1) {     printf("Please enter the even # of rows (4,6,8,10,12,14,16,18,20):");     scanf("%d", &row);     printf("Please enter the even # of columns (4,6,8,10,12,14,16,18,20):");     scanf("%d", &column);     if ((row>20) || (row<4) || (column>20) || (column<4) || !(row%2==0) || !(column%2==0))                 printf("Invalid Data\n");                 else                 q1=0; }    board[20][20]=boardgame(row,column);  printf("\n\n\n");     /* Prints the board */     for (z=0; z<row; z++) {         printf("%d", row-z);     for (x=0; x<column; x++) {                 printf("  %c  ", board[z][x]); }                 printf("\n\n"); }                                 printf(" ");                 for (z=1; z<=column; z++)                 printf("  %d  ", z);                 printf("\n\n\n");             system("PAUSE");     return EXIT_SUCCESS; }```
• 03-07-2010
anon
You can't assign arrays, nor return them from a function.

Code:

`board[20][20]=boardgame(row,column);`
This just assigns an integer to an item that also happens to be out of bounds.

Instead you should pass the board into the function to be filled out there.

Code:

`int boardgame(int row, int column, int board2[][20])`
The filling in part also looks messed up. I thought you wanted one of 4 random characters in each field, and then put a diamond in the middle.

Code:

```char arrows[4] = {x, y, z, w}; //whichever values the arrows should be char random_arrow = arrows[rand() % 4];```