//動態配置結構陣列，
//讓使用者輸入n後，
//先讓使用者依序輸入n個學生姓名、身高、體重
//算出身高及體重平均，並輸出平均
//最後再輸出身高或體重任一項(一項兩項都可以)低於平均的學生姓名
//名字最長不會超過20字元

//承上題，
//輸入改為由檔案輸入
//輸出改為輸出至檔案
//程式流程：
//先後讀取輸入檔名與輸出檔名後，過程如上題，
//檔名不會超過50字元，程式畫面如下圖，
//讀入檔名時請分兩次（兩次 gets 或 scanf）

//測試資料可以在課程網頁下載
//內容有：
//輸入檔：可用複製貼上至黑黑的那個視窗
//輸出檔：答案，請仔細比較


#include <stdio.h>
#include <stdlib.h>

struct student{
    	char name[21]; 
		int height, weight;
	};

int main(){
    FILE *in, *out;  
	int i, n;
	char str[51], string[51];
    double sum_h = 0, sum_w = 0, H_avg, W_avg;
      
    printf("Input file:%s", str);
    scanf("%s", &str);
    
    in = fopen(str, "r");  //
      
   	if (in == NULL) {
        printf("open input file failed!\n");
        return 0;
    }
    
    printf("Output file:%s", string);
    scanf("%s", &string);
    
    out = fopen(string, "w"); //
       
	fscanf(in, "%d", &n);

	struct student *a = (struct student *)malloc(sizeof(struct student)*n);

	for (i = 0; i < n; i++){
	    fscanf(in, "%s %d %d", a[i].name, &a[i].height, &a[i].weight);
	}

	for (i = 0; i < n; i++){
        sum_h += a[i].height;
	    sum_w += a[i].weight;
	}

	H_avg = sum_h / n;
	W_avg = sum_w / n;

	fprintf(out, "H_avg:%.2lf\n", H_avg);
	fprintf(out, "W_avg:%.2lf\n", W_avg);
    
    for (i = 0; i < n; i++){
		if(a[i].height < H_avg || a[i].weight < W_avg){
			fprintf(out, "%s\n", a[i].name);
	//動態配置結構陣列，
//讓使用者輸入n後，
//先讓使用者依序輸入n個學生姓名、身高、體重
//算出身高及體重平均，並輸出平均
//最後再輸出身高或體重任一項(一項兩項都可以)低於平均的學生姓名
//名字最長不會超過20字元

//承上題，
//輸入改為由檔案輸入
//輸出改為輸出至檔案
//程式流程：
//先後讀取輸入檔名與輸出檔名後，過程如上題，
//檔名不會超過50字元，程式畫面如下圖，
//讀入檔名時請分兩次（兩次 gets 或 scanf）

//測試資料可以在課程網頁下載
//內容有：
//輸入檔：可用複製貼上至黑黑的那個視窗
//輸出檔：答案，請仔細比較


#include <stdio.h>
#include <stdlib.h>

struct student{
    	char name[21]; 
		int height, weight;
	};

int main(){
    FILE *in, *out;  
	int i, n;
	char str[51], string[51];
    double sum_h = 0, sum_w = 0, H_avg, W_avg;
      
    printf("Input file:%s", str);
    scanf("%s", &str);
    
    in = fopen(str, "r");  //
      
   	if (in == NULL) {
        printf("open input file failed!\n");
        return 0;
    }
    
    printf("Output file:%s", string);
    scanf("%s", &string);
    
    out = fopen(string, "w"); //
       
	fscanf(in, "%d", &n);

	struct student *a = (struct student *)malloc(sizeof(struct student)*n);

	for (i = 0; i < n; i++){
	    fscanf(in, "%s %d %d", a[i].name, &a[i].height, &a[i].weight);
	}

	for (i = 0; i < n; i++){
        sum_h += a[i].height;
	    sum_w += a[i].weight;
	}

	H_avg = sum_h / n;
	W_avg = sum_w / n;

	fprintf(out, "H_avg:%.2lf\n", H_avg);
	fprintf(out, "W_avg:%.2lf\n", W_avg);
    
    for (i = 0; i < n; i++){
		if(a[i].height < H_avg || a[i].weight < W_avg){
			fprintf(out, "%s\n", a[i].name);
		}
	}
	
    fclose(in);                                  
	fclose(out); 
                                    
	free(a);
	
	system("pause");
	return 0;
}


	}
	}
	
    fclose(in);                                  
	fclose(out); 
                                    
	free(a);
	
	system("pause");
	return 0;
}


