Printing a 2D array in C

后端 未结 3 1433
礼貌的吻别
礼貌的吻别 2020-12-15 08:28

how would I print a 2d array in c using scanf for user input, array called grid[ ][ ] and a for loop?

say if the user types in 3 5, the out

相关标签:
3条回答
  • 2020-12-15 09:05

    First you need to input the two numbers say num_rows and num_columns perhaps using argc and argv then do a for loop to print the dots.

    int j=0;
    int k=0;
    for (k=0;k<num_columns;k++){
       for (j=0;j<num_rows;j++){
           printf(".");
       }
     printf("\n");
     }
    

    you'd have to replace the dot with something else later.

    0 讨论(0)
  • 2020-12-15 09:15

    Is this any help?

    #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;
    }
    
    0 讨论(0)
  • 2020-12-15 09:29
    ...
    for(int i=0;i<3;i++){ //Rows
    for(int j=0;j<5;j++){ //Cols
     printf("%<...>\t",var);
    }
    printf("\n");
    }
    ...
    

    considering that <...> would be d,e,f,s,c... etc datatype... X)

    0 讨论(0)
提交回复
热议问题