(1251781355さん)
●使用した入力ファイル(a0.txt)
0.331248 0.365062 0.259407 0.664632 0.457198
0.736137 0.385998 0.106632 0.042879 0.113010
0.245827 0.377819 0.210395 0.067629 0.809320
0.008972 0.541643 0.831324 0.951292 0.168950
0.649800 0.705527 0.843867 0.611438 0.963683
例:
#include<stdio.h>
#include<stdlib.h>
#define infile "a0.txt"
int comp_d(const void *a, const void *b) {
return *(double*)a < *(double*)b;
}
int main(void) {
FILE *fp;
double *p;
double v, sum=0;
int i, n=0;
if((fp=fopen(infile, "r"))==NULL) {
puts("file open error");
return 1;
}
while(fscanf(fp, "%lf", &v)==1)n++;
p=(double*)malloc(sizeof(double)*n);
fp=freopen(infile, "r", fp);
n=0;
while(fscanf(fp, "%lf", &v)==1) {
sum += v;
p[n++]= v;
}
fclose(fp);
qsort(p, n, sizeof(double), comp_d);
for(i=0; i<n; i++) {
printf("%f\n", p[i]);
}
puts("--------------");
printf("sum = %f\n", sum);
free(p);
return 0;
}
ーーー実行結果ーーーーーーーーーー
0.963683
0.365062
0.611438
0.664632
0.457198
0.736137
0.385998
0.843867
0.705527
0.649800
0.951292
0.377819
0.331248
0.831324
0.809320
0.541643
0.210395
0.259407
0.245827
0.168950
0.113010
0.106632
0.067629
0.042879
0.008972
--------------
sum = 11.449689