/* 程式功能: 輸入10名學生成績,列印學生成績總合及平均分數*/
#include <stdio.h>
#define STU_NUM 10

void ReadScore();
float compute();

float x[STU_NUM+1]; /* 存放學生成績的陣列 */
FILE *logfile; /* 存放中間過程的檔案指標  */
FILE *InputFile; /* 存放Input的檔案指標  */

void main()
{
float sum;
logfile = fopen("logfile.txt", "w");
        InputFile=fopen("InputFile.txt","r");

ReadScore(); //讀取10個學生成績
        sum=compute(); //計算學生成績總合

        printf("\n");
fprintf(logfile, "\n");

printf("sum = %f",sum);
        printf(logfile, "sum = %f\n", sum);

printf("\n");
fprintf(logfile, "\n");

printf("average=%f", sum/STU_NUM);
        fprintf(logfile, "average=%f",sum/STU_NUM);

fclose(logfile);
        fclose(InputFile);

}


/* 讀取10個學生成績 */
void ReadScore()
{
int i;

        printf("開始由InputFile.txt讀成績.....");
        fprintf(logfile,"開始由InputFile.txt讀成績.....");
        printf("\n");
        fprintf(logfile,"\n");
        for(i=1;i<=STU_NUM;i++)
       {
fscanf(InputFile,"%f ",&x[i]);
                printf("x[%d]= %f ",i, x[i]);
                printf("\n");

                fprintf(logfile,"x[%d]= %f",i, x[i]);
                fprintf(logfile,"\n");

}//end for

}


/* 計算學生成績總合 */
float compute()
{
int i;
float sum = 0;

for (i=1; i<=STU_NUM; i++)
{
sum =sum+x[i];//累加10個學生成績
            printf("累加成績 sum(%d)= %f", i, sum);
                fprintf(logfile,"累加成績 sum(%d)= %f", i, sum);
                printf("\n");
                fprintf(logfile,"\n");
}

        return sum;
}