**这是一个被删除的问题,但我修改了一下,这样这个社区就更容易理解我的要求了
这是我的代码:
#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的"Enter Row 0“"Enter Row 1”,我将如何使它成为用户输入两行的一组数字。当我编译时,它只是不停地说“输入0行:输入0行:输入0行”。程序应该知道在0到9之间输入了多少次数字。最终结果应该如下所示
输入数组大小:2 6 输入0: 0 1 2 3 4 5 输入第1行:0 1 6 7 8 9 每一位数的总数: 数字0发生2次 数字1发生2次 数字2发生1次 数字3发生1次
ect。这会一直持续下去,直到程序命中“数字9”发生了很多次。
当我在没有printf的情况下编译时,它会运行3行,当它应该是2时,编译器给出的大多数数字都是wack的,只有2位数除外。
如果数字1发生3次
数字2发生-343589435次
谢谢你的帮助!
发布于 2017-10-15 23:52:50
有10位数0-9.因此,数组count应该有十个元素。此外,还需要初始化数组。
要么使用具有常量表达式指定大小的数组,然后在如下的声明中对其进行初始化
#define N 10
//...
int count[N] = { 0 };或者使用可变长度数组并使用标头<string.h>中声明的函数<string.h>初始化它,例如
#include <string.h>
//...
int i = 10;
int count[i];
memset( count, 0, i * sizeof( int ) );否则,如果数组未初始化,它将具有不确定的值。
https://stackoverflow.com/questions/46761095
复制相似问题