【问题标题】:how to find the min/max of values within a file如何在文件中查找最小值/最大值
【发布时间】:2017-06-09 22:25:48
【问题描述】:
#include <stdio.h>
int main(void)
{
    int num, i, total, average, min, max;
    min = num;
    max = num;
    FILE *ifile;
    ifile = fopen("scores.txt", "r");

    i = total = 0;
    while (fscanf(ifile, "%d", &num) != EOF) {
        i++;
        total += num;
    }

    printf("The total of the integers is %d.\n", total);
    printf("The number of integers in the file is %d.\n", i);

    average = total/i;

    printf("The average of the integers is %d.\n", average);

    while (fscanf(ifile, "%d", &num) != EOF) {
        if (num < min) {
            printf ("The minimum is %d\n", min);
        } else if (num > max) {
            printf ("The maximum is %d\n", max);
        }
    }

    fclose(ifile);
    return (0);
}

错误的代码部分是关于最小值/最大值的结尾。 我不确定是否为此添加一个循环,甚至自己制作 min 和 max 变量。

【问题讨论】:

  • min = num; max = num; 这里,num 未初始化。在第一次迭代的任一循环中将minmax 初始化为文件中的任何值,然后比较它们。
  • min = num;这行代码中num的值是多少?
  • 我试图将文件中的 num 设为数字

标签: c max min


【解决方案1】:

您的最小/最大检测循环中至少存在三个问题:

首先,如chux所示,minmax没有被初始化;因此,在遍历数字时,声明 if (num &lt; min)... 远不能保证正常工作(当然,max 也是如此)。 所以用INT_MAX初始化min,用INT_MIN初始化max(都在&lt;limits.h&gt;中定义),这样已经在第一次迭代中minmax将被设置为文件中的值.

其次,检查if (num &lt; min)... 会为您提供“本地”最小值,即到目前为止读取的所有数字的最小值,但不是“绝对”最小值,因为稍后可能会出现较小的数字。所以 min/max 将在循环结束时有效,而不是在迭代期间。

第三,if (num &lt; min) ... else if (num &gt; max) 至少在文件只包含一个数字时是错误的。

您的代码可能如下所示:

int min = INT_MAX;
int max = INT_MIN;
while (fscanf(ifile, "%d", &num) != EOF) {
    if (num < min)
        min = num;
    if (num > max)
        max = num;
}
// min/max are valid earliest at this point:
printf ("The minimum is %d\n", min);
printf ("The maximum is %d\n", max);

// Note that min/max will not be correct if the file does not contain any number;
// Note further, that fscanf(ifile, "%d", &num) != EOF may result in an endless loop if the file contains characters that "%d" will not read as a valid integral value.
// But this is left to the OP for further improvement :-)

【讨论】:

    【解决方案2】:

    考虑对您的代码进行以下修改:

    #include <stdio.h>
    
    int main(void) {
        int num, i, total, average, min, max;
    
        // open file
        FILE *ifile = fopen( "scores.txt", "r" );
        // if opening file fails, print error message and exit 1
        if (ifile == NULL) {
            perror("Error: Failed to open file.");
            return 1;
        }
    
        // initialize i and total to zero.
        i = total = 0;
        // read values from file until EOF is returned by fscanf,
        //   or until an error (hence the comparison "== 1")
        while(fscanf(ifile, "%d", &num) == 1) {
            // In first round of loop, initialize min and max
            //   to the number being read (num)
            // After min/max have been initialized, they can then
            //   be compared and modified in the second if statement 
            //   in the remaining loop rounds
            if (i == 0) {
                min = num;
                max = num;
                total += num;
                ++i;
                continue;
            } 
            if (num < min) {
                min = num;
            } else if (num > max) {
                max = num;
            }
            total += num;
            ++i;
        }
    
        // initialize average
        average = total/i;
    
        // summary
        printf("The sum of all integers in file is %d.\n", total);
        printf("The number of integers in file is %d.\n", i);
        printf("The average of all integers in file is %d.\n", average);
        printf ("The minimum is %d\n", min);
        printf ("The maximum is %d\n", max);
    
        fclose(ifile);
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 2012-09-16
      • 2013-11-12
      • 2021-11-10
      • 1970-01-01
      • 2016-01-05
      • 2017-04-20
      相关资源
      最近更新 更多