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 output will be:
.....
.....
.....
Here is the code that I have written so far (newbie here):
#include <stdio.h>
#define MAX 10
int main()
{
int grid[MAX][MAX];
int row, col;
int i,j;
printf("Please enter your grid size: ");
scanf("%d %d", &row, &col);
for (i=0; i<MAX; i++)
for //i gave up here
}
This is only a little part of the whole stage of my task:
Enter number of rows and columns followed by list of words (hit enter twice to end list): 10 15
quick
brown
fox
jumped
over
lazy
dog
00 . . . . . . . . . . . . . . .
01 . . . . . . . . . . . . . . .
02 . . . . . . . . . . . . . . .
03 . . . . . . . . . . . . . . .
04 . . . . . . . . . . . . . . .
05 . . . . . . . . . . . . . . .
06 . . . . . . . . . . . . . . .
07 . . . . . . . . . . . . . . .
08 . . . . . . . . . . . . . . .
09 . . . . . . . . . . . . . . .
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
0. quick
1. brown
2. fox
3. jumped
4. over
5. lazy
6. dog
functions allowed and should be included in the code: string functions - strlen(),strcpy(), strcat(), strchr(), strcmp(),strstr()
must use 2d array
must use fgets for words. Out put must match the exact format.
This question is related to
multidimensional-array
...
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)
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.
Source: Stackoverflow.com