[其他] 學寫C功課一問

Question:
The global climate change has become very severe. A place call Rainland used to have rain everyday is now suddenly becoming dry. Some days may have little rain and other days have no rain at all. Doris is asked to check the rainfall data and see if it is the case. Help her to write a simple analysis program based on LoopWhile8.c (Unit 2 Page 58). The program should read the period of analysis (number of days). The program should then read the amount of rainfall (in millimeters) each day. The program should finally print the following results:
 Average rainfall (in millimeters).
 Maximum rainfall (in millimeters).
 Minimum rainfall (in millimeters).
 Number of days of which the rainfall is 0.
 Number of days of which the rainfall is less than 5 millimeters.
 Number of incidents when there are two consecutive days recording 0 rainfall.

my answer:
#include<stdio.h>
#include<climits>
  int main(){
  int days, i, zero, small, consecutive;
  float average, sum, mm, min, max;
  sum=0;
  zero=0;
  small=0;
  mm=0;
  consecutive=0;
  max=INT_MIN;
  min=INT_MAX;
  printf("Enter number of days of the rainfall analysis period: ");
  scanf("%d",&days);
  for(i=1;i<=days;i++) {
  printf("Enter the rainfall on day %d (mm): ",i);
  scanf("%f",&mm);
  sum+=mm;
  average=sum/days;
  if(mm>max){
  max=mm;}
  if(mm<min){
  min=mm;}
  if(mm==0){
  zero++;}
  if(mm<5){
  small++;}
  }
  printf("Average rainfall in the period: %f\n", average);
  printf("Maximum daily rainfall is %f\n", max);
  printf("Minimum daily rainfall is %f\n", min);
  printf("Number of days with zero rainfall: %d\n", zero);
  printf("Number of days with low rainfall (< 5 mm): %d\n", small) ;
  printf("Number of incidents with consecutive days with zero rainfall: %d", consecutive) ;
  fflush(stdin);
  getchar();
  }

要計連續2日都係0諗唔到.......
用array count?

loop裏面加多個variable mmyesterday去記着昨日的雨量
然後check ((mmyesterday == 0) && (mm == 0))

TOP

本帖最後由 henrietta 於 2014-10-28 21:31 編輯
loop裏面加多個variable mmyesterday去記着昨日的雨量
然後check ((mmyesterday == 0) && (mm == 0)) ...
smoke_cheese 發表於 2014-10-28 20:50

#include<stdio.h>
#include<climits>
  int main(){
  int days, i, zero, small, consecutive;
  float average, sum, mm, min, max,mmyesterday;
  sum=0;
  zero=0;
  small=0;
  mm=0;
  consecutive=0;
  max=INT_MIN;
  min=INT_MAX;
  printf("Enter number of days of the rainfall analysis period: ");
  scanf("%d",&days);
  for(i=1;i<=days;i++) {{
  printf("Enter the rainfall on day %d (mm): ",i);
  scanf("%f",&mm);
  sum+=mm;
  mmyesterday=mm;
  average=sum/days;
  if(mm>max){
  max=mm;}
  if(mm<min){
  min=mm;}
  if(mm==0){
  zero++;}
  if(mm<5){
  small++;}
  }
  if((mmyesterday == 0) && (mm == 0)){
  consecutive++;
  }
  }
  printf("Average rainfall in the period: %f\n", average);
  printf("Maximum daily rainfall is %f\n", max);
  printf("Minimum daily rainfall is %f\n", min);
  printf("Number of days with zero rainfall: %d\n", zero);
  printf("Number of days with low rainfall (< 5 mm): %d\n", small) ;
  printf("Number of incidents with consecutive days with zero rainfall: %d", consecutive) ;
  fflush(stdin);
  getchar();
  }
但係點記着昨日的雨量?

TOP

但係點記着昨日的雨量?
henrietta 發表於 2014-10-28 21:30

響scanf("%f",&mm);之前mm裝着甚麼?

TOP

響scanf("%f",&mm);之前mm裝着甚麼?
smoke_cheese 發表於 2014-10-28 22:04

要小心第一次loop(當 i=1)

仲有,#include<climits>係C++,唔係C

TOP

響scanf("%f",&mm);之前mm裝着甚麼?
smoke_cheese 發表於 2014-10-28 22:04

明白了,謝謝
一直諗多左......

TOP

要留意此案例,mmyesterday 初始為 0, 而且第一次的降雨量亦為 0,
須然只有一天, 但 consecutive 變量會加咗 1。

TOP