namereq

randnum

Jul 10th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. int main(void)
  5. {
  6.     FILE *fp;
  7.     fp = fopen("randnum.txt","w");
  8.     srand((unsigned int)time(NULL));
  9.     int i, N, k;
  10.     N = rand() % 9998 + 3;
  11.     float sum, arr[10000];
  12.     for(i=0;i<N;i++){
  13.         fprintf(fp, "%.2f\n", ((rand() % 2001)-1000)/100.0 );
  14.     }
  15.    
  16.     fclose(fp);
  17.    
  18.     // 読み込んで平均を出す部分
  19.     fp = fopen("randnum.txt","r");
  20.     k = 0;
  21.     sum = 0.0;
  22.     while (fscanf(fp, "%f", &arr[i]) != EOF){
  23.         sum += arr[i];
  24.         k++;
  25.     }
  26.     printf("平均は、%f\n",sum/k);
  27.    
  28.     fclose(fp);
  29.    
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment