用C打印2D数组 [英] Printing a 2D array in C
本文介绍了用C打印2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将如何使用scanf在c 中使用用户输入的二维打印数组,名为grid [] []和for循环的数组?
how would I print a 2d array in c using scanf for user input, array called grid[ ][ ] and a for loop?
假设用户输入3 5,则输出将为:
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
允许并应该包含在代码中的函数:字符串函数-strlen(),strcpy(),strcat(),strchr(),strcmp(),strstr()
functions allowed and should be included in the code: string functions - strlen(),strcpy(), strcat(), strchr(), strcmp(),strstr()
必须使用2d数组
必须使用fgets作为单词.输出必须匹配确切的格式.
must use fgets for words. Out put must match the exact format.
推荐答案
这有帮助吗?
#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;
}
这篇关于用C打印2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文