2017-06-03 64 views
-1

請幫我找到最高溫度和最低溫度。我想我錯過了一些東西來使它成功。它快完成了。如果創建條件,我需要使用嗎? 請幫我找到最高和最低溫度。我想我錯過了一些東西來使它成功。它快完成了。如果創建條件,我需要使用嗎?詢問溫度分析儀程序c

#include <stdio.h> 
#define NUMS 3 

int main(void) { 

int high; 
int low; 
int totalhigh=0; 
int totallow=0; 
double average; 
int i; 
printf("---=== Temperature Analyzer ===---\n"); 
for (i = 0; i < 3; i++) 
{ 
printf("Enter the high temperature for the day %d: ", i + 1); 
scanf("%d", &high); 
printf("Enter the low temperature for the day %d: ", i + 1); 
scanf("%d", &low); 
totalhigh = high + totalhigh; 
totallow = low + totallow; 
    } 

average = (double)(totalhigh + totallow)/6 ; 
printf("The average (mean) temperature was: %.2lf\n", average); 
return 0; 
} 
+1

'INT最大= INT_MIN;'...'如果(最大<高)最大=高;' – BLUEPIXY

+1

聲明兩個變量:'min'初始化爲'INT_MAX'和'max'初始化爲'INT_MIN' ,然後檢查每次迭代,如果當前值爲'< min' or '> max' –

+1

與您的問題無關,但由於這三個while循環都具有相同的代碼,所以爲什麼不在一個循環中使用三個條件或一起? – pstrjds

回答

0

您需要記錄創建的最高(以及最低)。

struct hit 
{ 
    int value, 
     day; 
}; 
struct hit highest = {-40; 0}, lowest = {+40; 0}; 
.... 
if (high > highest.value) 
{ 
    highest.value = high; 
    highest.day = i; 
} 
... 

最後你可以打印創辦值:

print("The highest has been %d on day %d\n", highest.value, highest.day); 
+0

我認爲你的日子輸出是關閉的。 – Yunnosch

+0

你是對的Yunnosh。謝謝 –

+1

循環爲0到2,天數爲1到3,第一天用i == 0輸入第一天。打印的問題使用i + 1。你存儲我並輸出沒有+1。對我來說,似乎依然如此。 – Yunnosch

1

不從,而我打字這趕到的意見不同。

#include <stdio.h> 
#define NUMS 3 

int main(void) { 

    int high; 
    int low; 
    int highest =-42; 
    int highday = -1; 
    int lowday = -1; 
    int lowest = 42; 
    int totalhigh=0; 
    int totallow=0; 
    double average; 
    int i; 

    printf("---=== IPC Temperature Analyzer ===---\n"); 
    for (i = 0; i < 3; i++) 
    { 
     printf("Enter the high value for day %d: ", i + 1); 
     scanf("%d", &high); 
     printf("Enter the low value for day %d: ", i + 1); 
     scanf("%d", &low); 
     printf("\n"); 

     totalhigh = high + totalhigh; 
     totallow = low + totallow; 
     if (high>highest) 
     { highest=high; 
      highday=i+1; 
     } 
     if (low<lowest) 
     { lowest=low; 
      lowday=i+1; 
     } 
    } 

    average = (double)(totalhigh + totallow)/6 ; 

    printf("The average (mean) temperature was: %.2lf\n", average); 
    printf("The lowest   temperature was: %3d on day %d\n", lowest, lowday); 
    printf("The highest  temperature was: %3d on day %d\n", highest, highday); 
    return 0; 
} 
+0

與評論略有不同,好的。我無法拒絕使用42.而且我沒有改變輸入錯誤檢查,感覺它會覆蓋回答OP問題的部分。不過我同意。 – Yunnosch

+0

我拒絕了對這個答案的編輯,並沒有輕易做出決定。編輯通過刪除它所具有的小錯誤檢查功能縮短了代碼(這是一件好事)。後者是我不喜歡的。我們歡迎編輯來取代或更好地改進錯誤檢查器(使它們更有效)。徹底刪除它們是額外的變化(與OP自己的代碼相比),這增加了解決OP問題/問題的變化的清晰度。即使改進它們也是如此「變幻莫測」,但改進和評論的錯誤檢查器是一個有價值的目標,並且會被接受。 – Yunnosch

+0

該死!我只注意到,我拒絕的我的答案的編輯實際上反映了在OP的問題**中由OP **所做的更改。對不起,如果我在審查時注意到了這些細節,我會接受。關於如何解決這個問題的想法是受歡迎的。 – Yunnosch