可以计算数组A中有多少数字落在从低到高的范围内的函数。

时间:2014-03-13 02:04:28

标签: c arrays function

我为计算机科学入门课程完成了一个问题集,对于我应该对这个程序做什么,我有点困惑。问题是:在一个文件中,有一些运动竞赛参与者的分数列表。该列表按参与者注册参赛者的顺序排列,而不是按分数顺序排列。单独的文件包含一组范围。您的工作是编写一个程序,可以读取这两个文件中的数据,并打印一个表格,显示有多少参与者属于每个给定范围。

这个问题是逐字逐句复制的,但我对这个问题有几个疑问。如果您可以发表评论,说明您对以下内容的看法,那就太好了:

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告诉您数组中有多少个数字。并使用这两个函数构建和打印第二个文件中给出的每个类别中有多少参与者的表。

现在我试着通过它自言自语,现在我更加困惑了。也许我只是让问题变得更加困难。任何提示都会有帮助。感谢

2 个答案:

答案 0 :(得分:0)

  1.   

    计算阵列A中的数字从低到高的范围内,包括在内。

    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;
    }
    
  2.   

    使用这两个函数来构造和打印第二个文件中给出的每个类别中有多少参与者的表。

    您可以通过使用简单的循环来实现这一目标

    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[]

答案 1 :(得分:0)

不确定输入文件应该是什么样的,我猜测每行一个数字。

分数文件:

0
1
2
3
4
5
6
7
8
9
10
19
29
39
49
59
69
79
89
99
99
99
99
99
99
99
100

范围文件:

0
10
11
20
21
30
31
40
41
50
51
60
61
70
71
80
81
90
91
100

来源:

#include <stdio.h>
#include <stdlib.h>

int readNumbers(FILE* input, int A[], int N)
{
    int n = 0;
    while (n < N && !feof(input))
    {
        fscanf_s(input, "%d", &A[n]);
        n++;
    }
    return n;
}
int count(const int A[], int N, int low, int high) {
    int i = 0, numbers_in_range = 0;

    for (i = 0; i < N; i++) numbers_in_range += (A[i] >= low && A[i] <= high) ? 1 : 0;

    return numbers_in_range;
}
inline int exiting(char* reason)
{
    if(reason != 0) printf("%s\n", reason);
    printf("Usage: rangechecker \"scoresfilename\" \"rangesfilename\"\n");
    printf("exiting...\n");
    exit (-1);
}

int main(int argc, char** argv) {

    int* scores = 0;
    int* ranges = 0;

    int num_scores = 0, num_ranges = 0;

    FILE* scoresfptr;
    FILE* rangesfptr;

    switch (argc) {
        case 1:
        case 2:
        exiting(0);
        break;
        case 3:
        //verify files exist and open them
        if((scoresfptr = fopen(argv[1], "r")) == NULL || (rangesfptr = fopen(argv[2], "r")) == NULL)
        {
            exiting("One of the files does not exist or cannot be opened.");
        }
    break;
    default:
        exiting(0);
        break;
    }
    //kind of dumb, but because N is a passed value you kinda gotta
    //know N ahead of the call to readnumber or count
    //that, or I am just tired.
    int ch = 0;
    //assuming instructions are correct, a list, 1 number per line
    //will fail if extra newlines in file
    do {
        ch = fgetc(scoresfptr);
        if (ch == '\n')
            num_scores++;
    } while(ch != EOF);
    //now we have N, can call readnumbers.  Put file pointer back at start of file.
    rewind(scoresfptr);
    //Can also now allocate memory for A[].
    scores = (int*)calloc(num_scores, sizeof(int));
    //check return value to see if anything was read in
    if(readNumbers(scoresfptr, scores, num_scores) > 0) {
        //musta been something in the file
        //get the ranges
        ch = 0;
        //assuming instructions are correct, a list, 1 number per line
        do {
            ch = fgetc(rangesfptr);
            if (ch == '\n')
                num_ranges++;
        } while(ch != EOF);
        //now we have N, can call readnumbers.  Put file pointer back at start of file.
        rewind(rangesfptr);
        //Can also now allocate memory for A[].
        ranges = (int*)calloc(num_ranges, sizeof(int));
        //check return value to see if anything was read in
        if(readNumbers(rangesfptr, ranges, num_ranges) > 0) {
            //now we can call count
            //print header column
            printf("ranges\tscores in range\n");
            for (int i = 0, j = 0; i < num_ranges/2; i++, j += 2)
            {
                printf("%d-%d:\t%d\n",  *(ranges + j),
                                    *(ranges + j + 1),
                                    count(scores, num_scores,   *(ranges + j),
                                                                *(ranges + j + 1)));
            }
        }
        else {
            exiting("Ranges File empty.");
        }
    }
    else {
        exiting("Scores File empty.");
    }
    free(scores);
    free(ranges);
    fclose(scoresfptr);
    fclose(rangesfptr);
    return(0);
}

输出:

ranges  scores in range
0-10:   11
11-20:  1
21-30:  1
31-40:  1
41-50:  1
51-60:  1
61-70:  1
71-80:  1
81-90:  1
91-100: 8