簡體   English   中英

從 C 中的數組輸出最大值和最小值

[英]Outputting the maximum and minimum value from an array in C

我編寫了一個程序,要求用戶輸入四天的高溫和低溫。 在此之后,程序使用所有四天的輸入來計算平均溫度。 一切正常,但是,我需要讓程序確定並輸出最高高溫及其發生日期以及最低低溫及其發生日期。 到目前為止,這是我的代碼

#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

由於做這樣的事情會幫助你更好地理解循環,我決定只描述這些步驟。 上面的代碼只是對您當前代碼的改進,向您展示一種更簡單的方法將向您展示如何解決此類問題的不同視角。

我更新了我的代碼,使上面代碼中提到的 if 語句正常運行。 這里是:

    if (high[0] > high[1] && high[0] > high[2] && high[0] > high[3]) { // Check to see if day 1 has the highest temperature against days 2,3 and 4.

            printf ("The highest temperature was %d, on day 1\n", high[0]); // Output day 1 as the highest temperature and indicate the temperature value.
    }
    else if (high[1] > high[0] && high[1] > high[2] && high[1] > high[3]) { // Same function as the above function for day 1 except this is used for day 2.

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

    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]);
    }

// Switch out high values with low values in order to determine lowest temperature and its corresponding day.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM