0

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

7
  • The second option seems more clear to me. Is the pattern always the same or it depends on some conditions? Commented Jul 11, 2014 at 14:11
  • Well I felt that if I used a conditional statement in filling the arrays, it would be better but I suppose filling it myself seems easier...so I should remove the getchar? Commented Jul 11, 2014 at 14:15
  • 1
    Turn on compiler warnings before posting code here: In function ‘createBoard’: warning: statement with no effect [-Wunused-value], In function ‘createBoard’: warning: array subscript is above array bounds [-Warray-bounds], warning: ‘myArray’ is used uninitialized in this function [-Wuninitialized], … Commented Jul 11, 2014 at 14:19
  • You mean you want the array to be randomly filled with xyz? Commented Jul 11, 2014 at 14:22
  • your last printf seems to be in wrong place. And getchar; should have no effect, you want getchar();. In any case, I'd suggest you separate the filling of teh array from the output. Commented Jul 11, 2014 at 14:23

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@FiddlingBits; Yes. Editing that.
That actually is quite clear, thanks. I guess there was no need for an if statement
@user3820970 Your if case was actually always true, unless myArray[0][1] == 0.
0

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.

1 Comment

I did consider using the time feature.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.