Presentation is loading. Please wait.

Presentation is loading. Please wait.

2-D Arrays Example - Grades. Grades Problem StdNo and Mid, Lab, Fin grades are in a file called Grades.txt. 201211001756580 201211008778890 … Total grades.

Similar presentations


Presentation on theme: "2-D Arrays Example - Grades. Grades Problem StdNo and Mid, Lab, Fin grades are in a file called Grades.txt. 201211001756580 201211008778890 … Total grades."— Presentation transcript:

1 2-D Arrays Example - Grades

2 Grades Problem StdNo and Mid, Lab, Fin grades are in a file called Grades.txt … Total grades is calculated as 30% of midterm and lab, and 40% of final grade Write a program that will read the grades of each students into an array, calculate the total grade and put the results in a file called GradesOut.txt.

3 Grades #include #define NSTDLMT 100 int main(void) { FILE *flinp, *flotp; int G[NSTDLMT][5]; int Status, i; flinp = fopen("Grades.txt", "r"); flotp = fopen("GradesOut.txt", "w"); i = 0; Status = fscanf(flinp, "%d%d%d%d", &G[i][0], &G[i][1], &G[i][2], &G[i][3]); while(Status != EOF) { G[i][4] = (int) (0.3 * G[i][1] * G[i][2] * G[i][3] + 0.5); fprintf(flotp, "%d %4d %4d %4d %4d\n", G[i][0], G[i][1], G[i][2], G[i][3], G[i][4]); i++; Status = fscanf(flinp, "%d%d%d%d", &G[i][0], &G[i][1], &G[i][2], &G[i][3]); } fclose(flinp); fclose(flotp); return(0); }

4 Grades with Functions #include #define NSTDLMT 100 intFillArr(FILE *flp, int grd[][5]); voidCalcGrd(int grd[][5], int r); voidPrntArr(FILE *flp, int grd[][5], int r); doubleFindAve(int grd[][5], int r, int k); int main(void) { FILE *flinp, *flotp; int G[NSTDLMT][5]; int N; flinp = fopen("Grades.txt", "r"); flotp = fopen("GradesOut.txt", "w"); N = FillArr(flinp, G); CalcGrd(G, N); PrntArr(flotp, G, N); fprintf(flotp, " Averages %.1f %.1f %.1f %.1f\n", FindAve(G, N, 1), FindAve(G, N, 2), FindAve(G, N, 3), FindAve(G, N, 4)); fclose(flinp); fclose(flotp); return(0); }

5 Grades with Functions intFillArr(FILE *flp, int grd[][5]) { int Status, i = 0; Status = fscanf(flp, "%d%d%d%d", &grd[i][0], &grd[i][1], &grd[i][2], &grd[i][3]); while(Status != EOF) { i++; Status = fscanf(flp, "%d%d%d%d", &grd[i][0], &grd[i][1], &grd[i][2], &grd[i][3]); } return(i); } voidCalcGrd(int grd[][5], int r) { int i; for(i=0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/3665760/12/slides/slide_4.jpg", "name": "Grades with Functions intFillArr(FILE *flp, int grd[][5]) { int Status, i = 0; Status = fscanf(flp, %d%d%d%d , &grd[i][0], &grd[i][1], &grd[i][2], &grd[i][3]); while(Status != EOF) { i++; Status = fscanf(flp, %d%d%d%d , &grd[i][0], &grd[i][1], &grd[i][2], &grd[i][3]); } return(i); } voidCalcGrd(int grd[][5], int r) { int i; for(i=0; i


Download ppt "2-D Arrays Example - Grades. Grades Problem StdNo and Mid, Lab, Fin grades are in a file called Grades.txt. 201211001756580 201211008778890 … Total grades."

Similar presentations


Ads by Google