0

I asked a much complicated question before here at Generating Unique Random Numbers in an Array using Loop

But I found out I just can't understand all the concepts yet and that there are too many things unknown, so I decided to learn it step by step.

So right now I am trying to create a 5x5 board using arrays with random numbers..here is my code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    //Declare the board size and other variables//

    //Create the random number generator seed

    //Loop to create the wanted board size

    //Plant the random numbers into the board within the loop

    int main()

    {
    //Initialize Variables
    int randomNumber;
    int rows;
    int columns;

    //Declare board size. Size of board is 5 x 5
    int board[5][5]; 

    //Create the random number generator seed
    srand(time(NULL));

    //Assign the random numbers from 1 - 25 into variable randomNumber
    randomNumber = rand() %25 + 1;

    //Create the rows for the board
        for ( rows = 1; rows <= 5 ; rows++ )
        {
            //Create the columns for the board
            for ( columns = 1; columns <= 5 ;  columns++ )
             {
             //Assign variable randomNumber into variable board
             board[randomNumber][randomNumber];
        }
            //Newline after the end of 5th column.
            printf("\n");
    }

    //Print the board
    printf("%d\t", board[randomNumber][randomNumber]);

}//end main

The last part board[randomNumber][randomNumber]; is where I think I got really confused. I really don't know what to do with it.

I'm trying to assign random numbers into the board, which I got it awfully wrong.

Any pointers guys?

Community
  • 1
  • 1
Sam Liew
  • 157
  • 2
  • 4
  • 15
  • Remember that arrays start at position 0. So, something like `int table[5]` contains items from `table[0]` to `table[4]`. Do Not access `table[5]`. – ChronoTrigger Oct 21 '13 at 14:27

1 Answers1

0

try something like this:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//Declare the board size and other variables//

//Create the random number generator seed

//Loop to create the wanted board size

//Plant the random numbers into the board within the loop

int main()

{
//Initialize Variables
int randomNumber;
int rows;
int columns;

//Declare board size. size of board is 5 x 5
int board[5][5]; 

//Create the random number generator seed
srand(time(NULL));

//Assign the random numbers from 1 - 25 into variable randomNumber

//Create the rows for the board
    for ( rows = 0; rows < 5 ; row++ )
    {
        //Create the columns for the board
        for ( columns = 0; columns < 5 ;  columns++ )
         {
         //Assign variable randomNumber into variable board
         randomNumber = rand() %25 + 1;

         board[rows][columns] = randomNumber;
         printf("%d\t", board[rows][columns]);

    }
        //Newline after the end of 5th column.
        printf("\n");
}

}//end main

you need to assign the generated number to the board by using the = Operator. What you did is just priting a random row and column in range 1-25 - so you would probably get a Exception here most of the time, and if it worked, your array is not filled and so has values by default (as your array is defined inside the method, there are some random values in there but you cant be sure what is in there until you filled it up with values)

reox
  • 5,036
  • 11
  • 53
  • 98
  • Hi reox, thanks for the quick reply. I was having trouble understanding how to get the randomNumber to go into the board and now I understand thanks to you! :) Cheers! – Sam Liew Oct 21 '13 at 14:49