replacing elements horizontally and vertically in a 2D array

Posted by wello horld on Stack Overflow See other posts from Stack Overflow or by wello horld
Published on 2010-04-30T05:25:02Z Indexed on 2010/04/30 12:07 UTC
Read the original article Hit count: 180

Filed under:
|

the code below ask for the user's input for the 2D array size and prints out something like this: (say an 18x6 grid)

..................
..................
..................
..................
..................
..................

code starts here:

#include <stdio.h>

#define MAX 10

int main()
{
    char grid[MAX][MAX];
    int i,j,row,col;

    printf("Please enter your grid size: ");
    scanf("%d %d", &row, &col);


    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            grid[i][j] = '.';
            printf("%c ", grid[i][j]);
        }
        printf("\n");
    }

    return 0;
}

I now ask the user for a string, then ask them where to put it for example:

Please enter grid size: 18 6
Please enter word: Hello
Please enter location: 0 0
Output:
Hello.............
..................
..................
..................
..................
..................
Please enter location: 3 4
Output:
..................
..................
..................
..Hello...........
..................
..................
program just keeps going.

Any thoughts on how to modify the code for this?

PS: Vertical seems way hard, but I want to start on horizontal first to have something to work on.

© Stack Overflow or respective owner

Related posts about c

    Related posts about homework