c - 从C中的数组中输出最大值和最小值

标签 c arrays max min

我编写了一个程序,要求用户输入四天内的高温和低温。在此之后,程序使用所有四天的输入计算平均温度。一切正常,但是,我需要让程序确定并输出最大高温及其发生的日期以及最小的低温及其发生的日期。到目前为止,这是我的代码

#include <stdio.h>
#define NUMS 4

int main (void)

{
int high[NUMS];
int low[NUMS];
const int MAX = 40;
const int MIN = -40;
int totalhigh;
int totallow;
int sum;
float avg;

    printf ("---===IPC Temperature Analyzer ===---\n");

    printf ("Enter the high value for day 1: ");
    scanf ("%d", &high[0]);

    printf ("Enter the low value for day 1: ");
    scanf ("%d", &low[0]);

    while (high[0] > MAX || low[0] <  MIN || high[0] < low[0]) {

    printf ("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.\n");

    printf ("Enter the high value for day 1: ");
    scanf ("%d", &high[0]);

    printf ("Enter the low value for day 1: ");
    scanf ("%d", &low[0]);

    }

    printf ("Enter the high value for day 2: ");
    scanf ("%d", &high[1]);

    printf ("Enter the low value for day 2: ");
    scanf ("%d", &low[1]);

    while (high[1] > MAX || low[1] < MIN || high[1] < low[1]) {

    printf ("Incorrect values, temperatures must be in the range -40 to 40,    high must be greater than low.\n");

    printf ("Enter the high value for day 2: ");
    scanf ("%d", &high[1]);

    printf ("Enter the low value for day 2: ");
    scanf ("%d", &low[1]);

    }

    printf ("Enter the high value for day 3: ");
    scanf ("%d", &high[2]);


    printf ("Enter the low value for day 3: ");
    scanf ("%d", &low[2]);

    }

    printf ("Enter the high value for day 4: ");
    scanf ("%d", &high[3]);

    printf ("Enter the low value for day 4: ");
    scanf ("%d", &low[3]);

    while (high[3] > MAX || low[3] < MIN || high[3] < low[3]) {

    printf ("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.\n");

    printf ("Enter the high value for day 4: ");
    scanf ("%d", &high[3]);

    printf ("Enter the low value for day 4: ");
    scanf ("%d", &low[3]);

    }

    totalhigh = high[0] + high[1] + high[2] + high[3];
    totallow = low[0] + low[1] + low[2] + low[3];

    sum = totalhigh + totallow;
    avg = sum/8.0;
    printf ("The average (mean) temperature was: %.2f\n", avg);

    if (high[0] > high[1] || high[0] > high[2] || high[0] > high[3]) {

            printf ("The highest temperature was %d, on day 1\n", high[0]);
    }
    else if (high[1] > high[0] || high[1] > high[2] || high[1] > high[3]) {

            printf ("The highest temperature was %d, on day 2\n", high[1]);
    }

    else if (high[2] > high[0] || high[2] > high[1] || high[2] > high[3]){
            printf ("The highest temperature was %d, on day 3\n", high[2]);
    }

    else {
            printf ("The highest temperature was %d, on day 4\n", high[3]);
    }


return 0;

}

最佳答案

您当前的代码可以使用一个循环和一个辅助函数,这将通过减少所有这些 scanf() 调用来缩短您的代码。你也可以通过使用更多的函数来抽象更多,但它会展示一般的想法。

检查 scanf() 的结果也很好,以防用户输入非整数。

您当前的代码可能如下所示:

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

#define NUMS 4

/* takes a pointer to a number */
void get_input(int *temp) {
    if (scanf("%d", temp) != 1) {
        printf("Invalid temp entered\n");
        exit(EXIT_FAILURE);
    }
}

int main(void) {
    int high[NUMS];
    int low[NUMS];
    const int MAX = 40;
    const int MIN = -40;
    int day = 1, totalhigh = 0, totallow = 0, sum;
    float avg;

    for (size_t i = 0; i < NUMS; i++) {
        printf ("Enter the high value for day %d: ", day);

        /* takes the address of the pointer given by get_input() */
        get_input(&high[i]);

        printf ("Enter the low value for day %d: ", day);
        get_input(&low[i]);
        while (high[i] > MAX || low[i] <  MIN || high[i] < low[i]) {
            printf ("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.\n");

            printf ("Enter the high value for day %d: ", day);
            get_input(&high[i]);

            printf ("Enter the low value for day %d: ", day);
            get_input(&low[i]);
        }
        day++;
    }

    for (size_t i = 0; i < NUMS; i++) {
        totalhigh += high[i];
        totallow += low[i];
    }

    sum = totalhigh + totallow;
    avg = sum/8.0;

    printf ("The average (mean) temperature was: %.2f\n", avg);

    return 0;
}

就查找最大和最小温度而言,您可以使用以下方法:

  • maxmin 设置为数组的第一个元素 array[0]
  • i=1 循环到 i=n
  • 如果元素大于max,将max设置为array[i]。如果元素小于 min,则将 min 设置为 array[i]
  • 最高和最低温度的日期为 i+1

因为这样做可以帮助您更好地理解循环,所以我决定只描述这些步骤。上面的代码只是对您当前代码的改进,向您展示一种更简单的方法将向您展示如何解决此类问题的不同视角。

关于c - 从C中的数组中输出最大值和最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42126951/

相关文章:

c - 使用 typedef 时如何分配内存?

c - 程序堆内存的碎片整理

arrays - 使用 Apache Pig 从文件中读取字符串数组

python Pandas : Best way to find local maximums in large DF

sql - 如何使用 MAX() 在 SQL 中多次出现 Max 值

c++ - 从 C 源文件调用 C++ 源文件中定义的全局函数

c - 如何在代码中使用白色

javascript - 选择二维数组中的 "lowest"项

C# 无法创建对象数组

c++ - Vector<double> C++ 的索引/最大值/最小值