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.
The 2D array represents a matrix. To print two dimensional or 2D array in C, we need to use two loops in the nested forms. The loops can be either for loop, while loop, do-while loop, or a combination of them. But understanding the syntax of for loop is easier compared to the while and do-while loop.
Code: public class Print2DArrayInJava { public static void main(String[] args) { //below is declaration and intialisation of a 2D array final int[][] matrx = { { 11, 22}, { 41, 52}, }; for (int r = 0; r < matrx. length; r++) { //for loop for row iteration. for (int c = 0; c < matrx[r].
public class Print2DArray { public static void main(String[] args) { final int[][] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; for (int i = 0; i < matrix. length; i++) { //this equals to the row in our matrix. for (int j = 0; j < matrix[i].
A two-dimensional array in C can be thought of as a matrix with rows and columns. The general syntax used to declare a two-dimensional array is: A two-dimensional array is an array of several one-dimensional arrays. Following is an array with five rows, each row has three columns: int my_array[5][3];
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With