【发布时间】:2017-10-15 23:37:45
【问题描述】:
**这是一个已删除的问题,但我重新制作了它,以便这个社区更容易理解我在问什么
这是我的代码:
#include <stdio.h>
#include <stdbool.h>
int main()
{
int row;
int col;
int x;
int y;
int i = 9;
int count[i];
printf("Enter the size of your array: ");
scanf("%d %d", &row, &col);
int arr[row][col];
//This will read the rows
for (int x = 0; x < row; ++x) {
for (int y = 0; y < col; ++y) {
printf("Enter row %d: ", x);
scanf("%d", &arr[x][y]);
}
}
//This will create a count for the rows
for (x = 0; x < row; ++x) {
for (y = 0; y < col; ++y) {
++count[arr[x][y]];
}
}
//This will count if digits repeat in the rows and print it
printf("\nTotal count for each digit:\n");
for (int j = 0; j < 10; ++j) {
printf("Digit %d occurs %d time%s\n", j, count[j], count[j] > 1 ? "s" : "");
}
return 0;
}
代码注释
我设置了 i = 9,因为用户应该输入的最大数字是 9 在“这将读取行”上应该有两个 printf “输入第 0 行” “输入第 1 行” 我将如何去做,以便用户输入一组数字供用户在两行中输入。当我编译它只是一直说“输入第 0 行:输入第 0 行:输入第 0 行”。该程序应该找出输入 0 到 9 之间的数字的次数。最终结果应该是这样的
输入数组的大小:2 6
输入第 0 行:0 1 2 3 4 5
输入第 1 行:0 1 6 7 8 9
每个数字的总计数:
数字 0 出现 2 次p>
数字 1 出现 2 次p>
第 2 位出现 1 次
第 3 位出现 1 次
等。这将一直持续下去,直到程序点击“数字 9 发生了很多次。
当我在没有 printf 的情况下编译时,它应该是 2 时运行 3 行,并且编译器给出的大多数数字都是古怪的,除了 2 位数字
例如数字 1 出现 3 次
第 2 位出现 -343589435 次
感谢您的帮助!
【问题讨论】:
-
int count[i];-->int count[10] = {0}; -
哇,它起作用了,您介意解释一下它做了什么以及为什么起作用吗?
-
十个元素(0..9)是必需的,必须用
0初始化。 -
所以我得到了随机数,因为所有这些数字都被初始化为内存中的随机数?
-
@chrisHG 除此之外,您还可以访问数组范围之外。