Structure Sort

# Structure Sort - big=s[i].avg; printf("The highest...

This preview shows pages 1–2. Sign up to view the full content.

//sort on avg-low to high #include <stdio.h> #define SIZE 4 struct Student { char name [20]; int t1,t2,t3; float avg; }; void load(struct Student s[],int n) { for(int i=0;i<n;i++) { printf("Enter name "); gets(s[i].name); printf("Enter 3 test scores "); scanf("%d%d%d",&s[i].t1,&s[i].t2,&s[i].t3); s[i].avg=(s[i].t1+s[i].t2+s[i].t3)/(float)3; fflush(stdin); } } void print(struct Student s[],int n) { for(int i=0;i<n;i++) printf("%s %d %d %d %f\n",s[i].name,s[i].t1,s[i].t2,s[i].t3,s[i].avg); } void sort(struct Student s[],int n) { int i,j; Student t; for(i=0;i<n-1;i++) for(j=0;j<n-1;j++) if(s[j].avg>s[j+1].avg) { t=s[j]; s[j]=s[j+1]; s[j+1]=t; } } void calcavgs(struct Student s[],int n) { int s1=0,s2=0,s3=0; float a1,a2,a3; for(int i=0;i<n;i++) { s1+=s[i].t1; s2+=s[i].t2; s3+=s[i].t3; } a1=s1/(float)n; a2=s2/(float)n; a3=s3/(float)n; printf("averages: %f %f %f\n",a1,a2,a3); } void havg(struct Student s[],int n) {

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
float big=s[0].avg; int i; for(i=1;i<n;i++) if(s[i].avg>big)
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: big=s[i].avg; printf("The highest average is %f\n",big); printf("The people with the highest averages are:\n"); for(i=0;i<n;i++) if(s[i].avg==big) printf("%s\n",s[i].name); } void main() { struct Student s[SIZE]; load(s,SIZE); print(s,SIZE); sort(s,SIZE); print(s,SIZE); calcavgs(s,SIZE); havg(s,SIZE); } /* Enter name John Enter 3 test scores 99 99 99 Enter name Steve Enter 3 test scores 88 88 88 Enter name Dave Enter 3 test scores 77 77 77 Enter name Eric Enter 3 test scores 66 66 66 John 99 99 99 99.000000 Steve 88 88 88 88.000000 Dave 77 77 77 77.000000 Eric 66 66 66 66.000000 Eric 66 66 66 66.000000 Dave 77 77 77 77.000000 Steve 88 88 88 88.000000 John 99 99 99 99.000000 averages: 82.500000 82.500000 82.500000 The highest average is 99.000000 The people with the highest averages are: John Press any key to continue . . . */...
View Full Document

## This note was uploaded on 10/05/2011 for the course CS 36 taught by Professor Sethhochwald during the Spring '09 term at Irvine Valley College.

### Page1 / 2

Structure Sort - big=s[i].avg; printf("The highest...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online