Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- struct stud
- {
- char name[25];
- float avg;
- int grade;
- };
- typedef struct stud stud_t;
- void average(FILE *pf)
- {
- int m,n;
- stud_t student[201],aux;
- int i=0,j;
- while(1)
- {
- if(fscanf(pf, "%[^,]", student[i].name)!=1)
- break;
- fgetc(pf);
- for(j=1;j<=10;j++)
- {
- if(fscanf(pf,"%d",&(student[i].grade))==1)
- student[i].avg=student[i].avg+student[i].grade;
- else
- break;
- fgetc(pf);
- if(student[i].grade<5){
- fscanf(pf,"*[^\n]");
- break;}
- }
- if(j==11)
- {
- student[i].avg /= 10;
- i++;
- }
- else
- student[i].avg = 0;
- }
- for(m=0;m<i;m++)
- for(n=m+1;n<=i;n++)
- if(student[m].avg<student[n].avg)
- {
- aux=student[m];
- student[m]=student[n];
- student[n]=aux;
- }
- for(m=0;m<i;m++)
- printf("The average of %s : %f\n",student[m].name,student[m].avg);
- }
- int main(void)
- {
- FILE *f;
- f=fopen("students.csv","r");
- if(!f){
- perror("Error on opening the file");
- return errno;}
- average(f);
- if(fclose(f)){
- perror("Error on closing the file");
- return errno;}
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement