Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int main(void)
- {
- FILE *fp;
- fp = fopen("randnum.txt","w");
- srand((unsigned int)time(NULL));
- int i, N, k;
- N = rand() % 9998 + 3;
- float sum, arr[10000];
- for(i=0;i<N;i++){
- fprintf(fp, "%.2f\n", ((rand() % 2001)-1000)/100.0 );
- }
- fclose(fp);
- // 読み込んで平均を出す部分
- fp = fopen("randnum.txt","r");
- k = 0;
- sum = 0.0;
- while (fscanf(fp, "%f", &arr[i]) != EOF){
- sum += arr[i];
- k++;
- }
- printf("平均は、%f\n",sum/k);
- fclose(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment