• 這個問題要給會的人來給答案

    Posted on 二月 7th, 2010 llythank 1 comment

    ~以亂數產生20天的氣溫資料(範圍從15℃~36℃),並且計算連續5天的平均溫度。其結果(資料檔)以檔案儲存!

    其中裡面似乎是不能做出平均的數值或者有其他問題而不能執行
    麻煩會的可以給一下詳細的解答ㄛ~謝囉!

    /* Fig. 5.9: fig05_09.c
    Randomizing die-rolling program */
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>

    /* function main begins program execution */
    int main()
    {
    srand(time(NULL));
    int i; /* counter */
    unsigned seed; /* number used to seed random number generator */

    seed; /* random number generator */

    /* loop 10 times */
    for ( i = 1; i <= 10; i++ ) {

    /* pick a random number from 1 to 6 and output it */
    printf( "%10d", rand() % 22 + 15 );

    /* if counter is divisible by 5, begin a new line of output */
    if ( i % 5 == 0 ) {
    printf( "\n" );
    } /* end if */

    } /* end for */
    system("pause");
    return 0; /* indicates successful termination */

    } /* end main */

    /**************************************************************************
    * (C) Copyright 1992-2004 by Deitel & Associates, Inc. and *
    * Pearson Education, Inc. All Rights Reserved. *
    * *
    * DISCLAIMER: The authors and publisher of this book have used their *
    * best efforts in preparing the book. These efforts include the *
    * development, research, and testing of the theories and programs *
    * to determine their effectiveness. The authors and publisher make *
    * no warranty of any kind, expressed or implied, with regard to these *
    * programs or to the documentation contained in these books. The authors *
    * and publisher shall not be liable in any event for incidental or *
    * consequential damages in connection with, or arising out of, the *
    * furnishing, performance, or use of these programs. *
    *************************************************************************/

     

    1 responses to “這個問題要給會的人來給答案” RSS icon

    • #include <stdlib.h>
      #include <stdio.h>
      #include <string.h>
      #include <time.h>

      int main()
      {
      srand(time(NULL));
      int sum=0;
      char szTmp[100],szSave[256];
      strcpy(szTmp,"");// initial
      strcpy(szSave,"");// initial

      FILE *fw=fopen("C:\\apple.txt","w");// Create a stream. Save to "C:\apple.txt"

      for(int i = 1; i <= 20; i++ )
      {
      int temp;
      printf("%10d", temp= rand() % 22 + 15 );// Show temperature
      sprintf(szTmp,"%10d",temp);
      strcat(szSave,szTmp);// Save temperature to szSave
      sum+=temp;

      if ( i % 5 == 0 )
      {
      sum = sum / 5;// average value
      printf( "\taverage = %d \n" , sum );// Show average temperature
      sprintf(szTmp,"\taverage = %d \n" , sum);
      strcat(szSave,szTmp);// Save average temperature to szSave
      sum=0;// Clear sum
      }

      }

      fputs(szSave,fw);// Write content to a stream.
      fclose(fw);// Closes a stream

      system("pause");

      return 0;
      }

      註︰
      1.資料檔以txt形式存在C:\apple.txt (可自行更改路徑,不過目錄要
      存在,斜線 \ 要改成雙斜線 \\。 e.g. "D:\\XXX\\XXX.txt
      2.有加註解,應該不難理解


    Leave a reply

    抱歉,您必須要登入才能發表迴響喔!