Write a program that will process semester grades and print a grade report for each student.
For each student the input will be:
For each student the output will be:
Calculations:
main:
get student id and number of courses while there are more students { print heading on grade report calculate GPA print grade report get student id and number of courses }
calculate gpa:
for each course { get course number, credits, grade grade points = grade points + credits * grade total credits = total credits + credits } gpa = grade points / total credits
print grade report:
How can we print the course number, credits and grade for each course now? We don’t have all of them anymore - we only have the values for the last course.
main:
get student id and number of courses while there are more students { print heading on grade report total credits = 0 grade points = 0 process courses calculate and print GPA get student id and number of courses }
process courses:
for each course { get course number, number of credits, grade print course number, number of credits, grade grade points = grade points + credits * grade total credits = total credits + credits }
calculate and print gpa:
gpa = grade points / total credits print a blank line print total credits, gpa
// process student grade data and print grade report for each // student read data from file student.dat. for each student, print // course data, then calculate and print gpa #include#include #include void heading(long); void processCourses(ifstream&, int, int&, float&); void calcAndPrintGpa(int,float); int main() { long studentid; int numcourses; int totalcredits; float gradepoints; ifstream students; // set up output to print float values cout << setiosflags(ios::fixed); cout << setiosflags(ios::showpoint); students.open("student.dat"); // each iteration of the loop processes one student students >> studentid >> numcourses; while (students) { heading(studentid); totalcredits = 0; gradepoints = 0; // set output to one decimal place for printing course grades, // then call func to read course info, print it, and calc total // credits and total gradepoints cout << setprecision(1); processCourses(students,numcourses,totalcredits,gradepoints); // set output to three decimal places for printing gpa, then call // func to calc and print the gpa cout << setprecision(3); calcAndPrintGpa(totalcredits,gradepoints); students >> studentid >> numcourses; } } // create heading for a student's grade report void heading(long studentid) { cout << "*************************************************\n\n"; cout << " Grade Report\n\n"; cout << " Student: " << studentid << endl << endl; cout << " Course\n"; cout << " Number Credits Grade\n\n"; } // loop through all courses for a given student: read course number, // credits, and grade, print them, and calc total credits and // gradepoints (gradepoints for a course are credits * grade) void processCourses(ifstream& students, int numcourses, int& totalcredits, float & gradepoints) { int i; float grade; int credits; int coursenum; for (i = 1 ; i <= numcourses ; i++) { students >> coursenum >> credits >> grade; cout << setw(7) << coursenum << setw(22) << credits << setw(20) << grade << endl; totalcredits = totalcredits + credits; gradepoints = gradepoints + credits * grade; } } // calc gpa and print total credits and gpa void calcAndPrintGpa(int totalcredits, float gradepoints) { float gpa; gpa = gradepoints / totalcredits; cout << "\nTotal credits:" << setw(10) << totalcredits; cout << endl << endl; cout << "GPA:" << setw(13) << gpa << endl << endl; }
11223344 3 133 4 3.5 129 4 3 121 3 4 88887777 2 133 4 4 121 3 3.5 99775533 3 133 4 3 125 3 2 121 3 2.5
************************************************* Grade Report Student: 11223344 Course Number Credits Grade 133 4 3.5 129 4 3.0 121 3 4.0 Total credits: 11 GPA: 3.455 ************************************************* Grade Report Student: 88887777 Course Number Credits Grade 133 4 4.0 121 3 3.5 Total credits: 7 GPA: 3.786 ************************************************* Grade Report Student: 99775533 Course Number Credits Grade 133 4 3.0 125 3 2.0 121 3 2.5 Total credits: 10 GPA: 2.550
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2002