#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
FILE*fp;
fp = fopen("random.dat","w");
if(fp==NULL){
return 1;
}
int count;
int a=100;
fprintf(fp,"%d\n",a);
srand((unsigned int)time(NULL));
for(count=0; count<a; count++){
fprintf(fp,"%d\n",rand()%10+1);
}
fclose(fp);
return 0;
}
2つ目
#include <stdio.h>
int main(int argc,char *argv[/url])
{
FILE *fp;
fp = fopen("random.dat", "r");
int j,total,num;
int count = 100;
for (total = j = 0; j < count; j++) {
fscanf(fp, "%d", &num);
total += num;
}
printf("AVE=%d\n", total / count);
fclose(fp);
return 0;
}
1つ目のプログラムで100個の1~10の乱数をrandom.datに出力させ、2つ目のプログラムでそのファイルを読み込み、合計値と平均値を出力するプログラムなのですが、
2つ目のプログラムで、malloc関数を使ってメモリを動的に読み込むようにすること。
読み込むメモリはrandom.datの先頭の数字を読み込むこと(この場合は100)。
random.datはプログラム内から読み込むのではなく、コマンドラインから読み込むこと。
と、この3つをやらなくてはなりません。
自分でも頑張ってみたのですが、時間の余裕があまりなく、ここに書き込ませていただきました。
ご助力お願いします。