我正在完成计算机科学入门类(class)的问题集,但我对我应该用这个程序做什么有点困惑。问题是:在一个文件中,有一些体育比赛参与者的得分列表。该名单按照参赛者报名参赛的顺序排列,而不是按照得分顺序排列。一个单独的文件包含一组范围。您的工作是编写一个程序,该程序可以读取这两个文件中的数据并打印一个表格,显示有多少参与者属于每个给定范围。
这个问题是逐字复制的,但我对此问题有几个疑问。如果您可以发表评论,表达您对以下内容的看法,那就太好了:
1) 好的,我编写了一些代码,以便从第一个数据文件中读取数字,将这些数字放入数组 A 中,并返回读取的数字数或 N(以较小者为准)。如下:
#include <stdio.h>
int readNumbers(FILE* input, int A[], int N)
{
int n;
n = 0;
while (n < N&&!feof(input))
{
fscanf_s(input, "%f", &A[n]);
n++;
}
return n;
}
int count(const int A[], int N, int low, int high){
}
2)我的困惑在于使用第二个函数来计算数组 A 中有多少数字落在从低到高的范围内(包括)。参数 N 告诉您数组中有多少个数字。并使用这两个函数构建并打印一个表格,其中显示第二个文件中给出的每个类别中有多少参与者。
现在我试图说服自己解决这个问题,但我现在更加困惑了。也许我只是让问题变得更加困难。任何提示都会有所帮助。谢谢
最佳答案
-
to count how many numbers in array A fall in the range from low to high, inclusive.
count()
可以这样实现int count(const int A[], int N, int low, int high){ int i, cnt; for (i = cnt = 0; i < N; i++) { if (low <= A[i] && A[i] <= high) cnt++; } return cnt; }
-
use these two functions to construct and print a table of how many participants are in each of the categories given in the second file.
您可以通过使用简单的循环来实现这一点
for (i = 0; i < number_of_categories; i++) { printf("%d - %d : %d\n", low[i], high[i], count(A, N, low[i], high[i])); }
假设您已将所有这些类别信息放入
low[]
和high[]
。
关于c - 函数可以计算数组 A 中有多少个数字落在从低到高的范围内。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22367690/