我正在尝试创建一个程序,使用函数和数组来统计每个数字(从0到9)发生的次数。以下是我得到的指示:
编写一个程序,要求用户输入一个NxM维数组,其中只包含0到9之间的数字,并计算10位数字中每一位出现在数组中的次数。
该方案应:
提示用户输入数组的大小(行和列),并在维度中读取。
提示用户逐行输入数组,并在每一行中读取,并验证用户是否输入了0到9之间的值。如果输入了此范围以外的值,程序将提示用户再次输入该行。
显示每个数字在数组中出现的总次数。
程序的输出应该如下所示(如果这有帮助的话):这个程序在NxM数组中计数数字0到9的次数。
*输入数组大小:2 6
*输入0: 0 1 2 3 4 5
*输入第1行:0 1 6 72 81 9
值超出范围。
*输入第1行:0 1 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;
}我的compute_row_count函数有点空,因为我不明白我是否遗漏了任何其他计算,或者有不准确的ones...once --我最终完成了compute_row_count和print_total_count --我应该能够从那里完成int (Void)。(因此不必担心在这部分帮助我)。然而,我确实需要在计算和print_total...so方面的认真帮助,请帮助我。
发布于 2015-10-27 07:10:27
#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;
}https://stackoverflow.com/questions/33358116
复制相似问题