rendered paste body/* * lab10q1.c * * * Created by Cristian Hancila on 15/04/07. * Copyright 2007 __MyCompanyName__. All rights reserved. * */#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct { char last_name[25]; int std_no; char major[15]; int numCourses; char course_nums[8][7]; int course_marks[8];} student;float studentAvarage(student s);int input_students(student ** s){ int count=0; int n=0; FILE * fp; s=malloc(sizeof(student *) * 25); fp=fopen("lab10.students.data.txt","r"); while(!feof(fp)) { s[n]=malloc(sizeof(student)); char c[15]; fscanf(fp,"%s",s[n]->last_name); //fscanf(fp,"%s",c); printf("%s\n",s[n]->last_name); fscanf(fp,"%d",&(s[n]->std_no)); fscanf(fp,"%s",s[n]->major); fscanf(fp,"%d",&(s[n]->numCourses)); int z = 0; for(z=0;z<s[n]->numCourses;z++){ fscanf(fp,"%s %d",s[n]->course_nums[z],&s[n]->course_marks[z]); } n++; count++; } printf("end of func call %s\n",s[0]->last_name); output_students(s,count); fclose(fp); return count;}void output_students(student ** s,int count){ int n=0; printf("\n Format:\n Last Name | Student Number | Major\n Course - Mark\n"); for(n=0;n<count-1;n++){ printf("%-15s %d %s\n\n",s[n]->last_name,s[n]->std_no,s[n]->major); int z; for(z=0;z<s[n]->numCourses;z++){ printf(" %s %d\n",s[n]->course_nums[z],s[n]->course_marks[z]); } printf("\n"); }}void majorAvarage(student s[], int count){ FILE * fp; char major[15]; int n = 0; int c=0; float msum=0; fp = fopen("lab10.majors.data.txt","r"); do{ fscanf(fp,"%s",major); msum=0; c=0; for(n=0;n<count-1;n++){ //printf("%s %s\n",major,s[n].major); if(strncmp(major,s[n].major,15)==0){ msum+=studentAvarage(s[n]); c++; } } if (c==0) c=1; msum=msum/c; printf("For major %s, avarage of %d students is %2.1f\n",major,c,msum); }while (!feof(fp)); fclose(fp);} float studentAvarage(student s){ int n =0; float sum=0; for (n=0;n<s.numCourses;n++){ sum+=(float)s.course_marks[n]; } float ret = sum/s.numCourses; printf(" %s's Avarage: %5.1f\n",s.last_name,ret); return ret;}int main(){ int count; student ** list; count=input_students(list); printf("%s\n",list[0]->last_name); //output_students(list,count); //majorAvarage(list,count); return 1;}