//ic210 section 4002 //16 nov 2009 /********************************************* This program reads in student/grade data from namedgrades.txt, gets a name from the user and prints out that student's homework average. **********************************************/ #include #include #include using namespace std; /********************************************* ** PROTOTYPES & STRUCT DEFINITIONS *********************************************/ struct Mid { string name; int* grades; } double computeAvg(int*, int); /********************************************* ** MAIN FUNCTION *********************************************/ int main() { // Open file and read header info int Ns, Nh; string junk; ifstream IN("namedgrades.txt"); IN >> Ns >> junk >> Nh >> junk; Mid* mids; //allocate space for mids mids = new Mid[Ns]; //allocate space for grades for each mid for (int k = 0; k < Ns; k++) { mids[k].grades = new int[Nh]; } //read mids for (int i = 0; i> mids[i].name; //read grades for (int j = 0; j < Nh ; j++) { IN >> mids[i].grades[j]; } } //find a student and compute the homework average string midName; cout << "Enter student name "; cin >> midName; double avg = 0; //find position of student for (int ii = 0; ii < Ns; ii++) { if (mids[ii].name == midName) { //exit the loop break; } } //output average if (ii < Ns) { cout << "Homework average for student "<< midName << " is " << computeAvg(mids[ii].grades, Nh); } else { cout << "Student not found" << endl; } return 0; } //compute the average of an array of grades double computeAvg(int* A, int size) { int sum = 0; for (int i = 0; i < size; i++) { sum = sum + A[i]; } double avg = (double)sum/size; return avg; }