user3820970
user3820970

Reputation: 3

Populating 2D array with if statement in C

new programmer needs advice. I've created a 5 by 5 board using 2d arrays. However I want to fill the array table with 4 possible letters x, y, z.

createBoard()
{
    char myArray[5][5];
    int i,j;
    for(i=0; i < ROW; i++){
        for(j=0; j < COL; j++){
            if(myArray[0][1]){
            myArray[i][j] = '0';
            printf("%c ", myArray[i][j]);
        }
    }
        printf("\n");
    }

    printf("%d\n", myArray[i][i]);
    getchar;
    return 0;
}

int main(int argc, char *argv[]){
    createBoard();
}

As of right now, all this does is fill the 5 by 5 with 0's. I want it to have a mix of x,y and z's. Would I be better of just filling each array myself like

char myArray[5][5] = 
{
{z,y,z,x,y},
{y,y,x,x,z},
}

and so on? or is there a better way considering later on I need to create functions to take in user input and manipulate the positions of the x, y, and z characters.

Cheers for any guidance

Upvotes: 0

Views: 2048

Answers (3)

John Bollinger
John Bollinger

Reputation: 180113

If you want createBoard always to set the same initial board, then it makes sense to do it with an array initializer:

char myarray[5][5] = {
    { 'x', 'y', 'z', 'x', 'y' },
    { 'z', 'x', 'y', 'z', 'x' },
    { 'y', 'z', 'x', 'y', 'z' },
    { 'x', 'y', 'z', 'x', 'y' },
    { 'z', 'x', 'y', 'z', 'x' }
}

But only if the array were a global variable or if you did not want to return it from the function. (And if you don't want to return the board, then what's the point of the function?)

If you intend ultimately to return the board from your function, either as a return value or via a pointer argument, then your function must create the value dynamically via one of the malloc() family of functions. E.g.

char myarray[5][5];

myarray = (char[5][5]) malloc(25 * sizeof(char));
/* now initialize */
...
/* later: */
return myarray;

In that case, you do not have the alternative of using an array initializer (at least in standard C). You must initialize by some other means, such as by copying the value of a compatible array or by setting the elements one by one.

Upvotes: 0

haccks
haccks

Reputation: 106012

Use rand function to randomly fill the array with letters x, y and z. Try this:

void createBoard()  // Place void as a return type as you are returning nothing from it.
{
    char myArray[5][5];
    char letter[3] = {'x', 'y', 'z'};
    int i,j;
    for(i=0; i < ROW; i++){
        for(j=0; j < COL; j++){
            myArray[i][j] = letter[rand()%3]; //rand()%3 will generate number from 0 to 2 randomly.
        }
    }
    for(i=0; i < ROW; i++){
        for(j=0; j < COL; j++){
            printf("%c ", myArray[i][j]);
        }
         printf("\n");
    }
    //Remove return 0 and getchar. No need of them.      
}  

Put srand(time(NULL)); in main to seed the rand function to get different result on each call of createBoard.

Upvotes: 2

MJVC
MJVC

Reputation: 507

Include the time header so you can use the random numbers:

#include <time.h>

You need to call srand before rand:

srand(time(NULL));

Put your possible values into an array:

char values[] = { '0', 'x', 'y', 'z' };

And you can get a random possible value with values[rand()%4]:

myArray[i][j] = values[rand()%4];

rand()%4 will give values beetween 0 and 3, your possible values indices.

Upvotes: 0

Related Questions