Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中计算NxM数组中0到9位数的程序?_C_Arrays_Function - Fatal编程技术网

在C中计算NxM数组中0到9位数的程序?

在C中计算NxM数组中0到9位数的程序?,c,arrays,function,C,Arrays,Function,我正在尝试创建一个程序,使用函数和数组计算每个数字(从0到9)出现的次数。以下是给我的指示: 编写一个程序,要求用户输入一个仅包含0到9之间的数字的NxM维度数组,并计算10个数字中每一个出现在数组中的次数 该计划应: ● 提示用户输入数组的大小(行和列),并读入尺寸 ● 提示用户逐行输入数组并在每行中读取,并验证用户输入的值是否介于0和9之间。如果输入的值超出此范围,程序应提示用户再次输入该行 ● 显示每个数字在数组中出现的总次数 程序的输出应如下所示(如果有帮助): 此程序统计NxM数组中数

我正在尝试创建一个程序,使用函数和数组计算每个数字(从0到9)出现的次数。以下是给我的指示:

编写一个程序,要求用户输入一个仅包含0到9之间的数字的NxM维度数组,并计算10个数字中每一个出现在数组中的次数

该计划应:

● 提示用户输入数组的大小(行和列),并读入尺寸

● 提示用户逐行输入数组并在每行中读取,并验证用户输入的值是否介于0和9之间。如果输入的值超出此范围,程序应提示用户再次输入该行

● 显示每个数字在数组中出现的总次数

程序的输出应如下所示(如果有帮助): 此程序统计NxM数组中数字0到9的出现次数

*输入数组的大小:2 6

*输入第0行:0 1 2 3 4 5

*输入第1行:0 1 6 72 81 9

值超出范围

*输入第1行:01 6 7 8 9

每个数字的总计数:

数字0出现2次

数字1出现2次

数字2出现1次

数字3出现1次

数字4出现1次

数字5出现1次

数字6出现1次

数字7出现1次

数字8出现1次

数字9出现1次

*用户输入

以下是我目前的计划:

#include<stdio.h>
#include<stdbool.h>

int read_row(int x, int y, int a[x][y]) /*function read_row reads in each row (including the prompt and retrieving the values). */
{
    printf("Enter the size of your array:");
    scanf("%d %d", &x, &y);
    a[x][y] = 10;    // x is the row count while y is the number of digits inputted
    while (x < 10 && y < 10)
    {
        printf("Enter row %d:", x);
        x++;
    }
    return x;
}

bool check_input(int x, int y) /*function check_input verifies that the user has not entered values outside the range 0 through 9. The function returns true if the range is ok and false otherwise. */

{
    if (x > 9)
        return false;
    else
        return true;
    if (y > 9)
        return false;
    else 
        return true;
}

int compute_row_count(int x, int y, int a[x][y], int count) /*function compute_row_count counts the number of time each digit occurs in an individual row. */
{
   a[x][y] = 0;
   for (x = 0; x < 10; x++)
    {
        for (y = 0; y < 10; y++)
        {
            while (y > 0)
            {
                count = y % 10;    // count reads and calculates the number of times a digit occurs in each row
                y /= 10;
            }
            a[y] = y;
        }


    }  
    return a[x][y];
}

int print_total_count(int x, int y, int a[x][y], int count) /*function print_total_count prints out the total count for each digit. Note the difference between printing “1 time” and “2 times”. */
{
    printf("Total count for each digit:\n");
    a[x][y] = 10;
        for (y = 0; y < 10; y++)
            {
                if (count <= 1)
                    printf("Digit %d occurs %d time\n", y, count);
                if(count >= 2 && y < 10)
                    printf("Digit %d occurs %d times\n", y, count);
            }
        return (y, count);
}

int main (void) /* I mainly need help on compute_row_total and print_total_count. Once I can figure those two out, I should be able to set this part up correctly, so no need to worry about this part */
{
int x, y, a[x][y], count;

read_row(x, y, a[x][y]);
if (check_input(x,y))
    printf("Values are outside of range.");
else 
    break;
compute_row_count(x, y, a[x][y], count);
print_total_count(x, y, a[x][y], count);


return 0;
}
#包括
#包括
int read_row(int x,int y,int a[x][y])/*函数read_row读取每一行(包括提示和检索值)*/
{
printf(“输入数组的大小:”);
scanf(“%d%d”,&x,&y);
a[x][y]=10;//x为行数,y为输入位数
而(x<10&&y<10)
{
printf(“输入第%d行:”,x);
x++;
}
返回x;
}
bool check_input(int x,int y)/*函数检查_input验证用户未输入0到9范围以外的值。如果范围为ok,则函数返回true,否则返回false*/
{
如果(x>9)
返回false;
其他的
返回true;
如果(y>9)
返回false;
其他的
返回true;
}
int compute_row_count(int x,int y,int a[x][y],int count)/*函数compute_row_count统计每个数字在单个行中出现的时间*/
{
a[x][y]=0;
对于(x=0;x<10;x++)
{
对于(y=0;y<10;y++)
{
而(y>0)
{
count=y%10;//count读取并计算一个数字在每行中出现的次数
y/=10;
}
a[y]=y;
}
}  
返回[x][y];
}
int print_total_count(int x,int y,int a[x][y],int count)/*函数print_total_count打印出每个数字的总计数。请注意打印“1次”和“2次”之间的差异*/
{
printf(“每个数字的总计数:\n”);
a[x][y]=10;
对于(y=0;y<10;y++)
{
如果(计数=2&&y<10)
printf(“数字%d出现%d次\n”,y,计数);
}
返回(y,计数);
}
int main(void)/*我主要需要关于计算行总数和打印行总数的帮助。一旦我能找出这两个,我应该能够正确地设置这一部分,所以不必担心这一部分*/
{
整数x,y,a[x][y],计数;
读取第(x,y,a[x][y]行);
如果(检查_输入(x,y))
printf(“值超出范围”);
其他的
打破
计算行数(x,y,a[x][y],计数);
打印总计数(x,y,a[x][y],计数);
返回0;
}
我把我的compute\u row\u count函数留为空,因为我不知道我是否遗漏了任何其他计算或有不准确的计算…一旦我完成compute\u row\u count并打印\u total\u count,我应该能够从那里完成int main(void)。(所以不用担心在这方面帮我)。但是,我确实需要在计算和打印总计方面得到认真的帮助…所以请帮助我。

\35;包括
#include <stdio.h>
#include <stdbool.h>

static inline bool check_input(int n){
    return 0 <= n && n <= 9;
}

void read_row(int x, int y, int a[x][y]){
    for (int r = 0; r < x; ++r){
        printf("Enter row %d: ", r);
        for (int c = 0; c < y; ++c){
            int value;
            if(1 != scanf("%d", &value) || check_input(value) == false){
                printf("Values outside of range.\n");
                while(getchar() != '\n'); //clear inputs;
                --r;
                break;
            }
            a[r][c] = value;
        }
    }
}

void compute_row_count(int x, int y, int a[x][y], int counts[]){
    //memset(counts, 0, sizeof(int)*10);
    for (int r = 0; r < x; ++r){
        for (int c = 0; c < y; ++c){
            ++counts[a[r][c]];
        }
    }
}

void print_total_count(int counts[]){
    printf("\nTotal count for each digit:\n");
    for (int i = 0; i < 10; ++i){
        printf("Digit %d occurs %d time%s\n", i, counts[i], counts[i] > 1 ? "s": "");
    }
}

int main (void){
    int x, y;
    printf("Enter the size of your array: ");
    scanf("%d %d", &x, &y);

    int a[x][y];
    int counts[10] = {0};

    read_row(x, y, a);
    compute_row_count(x, y, a, counts);
    print_total_count(counts);

    return 0;
}
#包括 静态内联布尔检查输入(int n){
返回0代码中有太多问题。编译器没有报告任何错误吗?我确实看到类似“预期的”int(*)[(sizetype)(y)]“但参数类型为”int'”和“传递”compute_row_count“的参数3使指针从整数开始,而不进行强制转换”的错误…但我没有使用指针?很抱歉,但我不清楚这些错误…我认为这个问题太宽泛了。我不是不礼貌,但您的代码表明您确实不了解2D数组在C中的工作方式(以及C编程的其他几个方面),SO帖子不是从头教你这个概念的地方。我想我所能建议的就是花更多的时间与你的课本和/或讲师在一起。在main中输入
x,y
。使用
检查
行内的
输入。