|
dhourd (Programmer) |
25 Feb 02 18:37 |
Cindy, this is a small program that should do the trick and allow you to enter up to 10 students. Keep in mind that a function called strtok() is used to parse the data. Also note that the program is easily configured to allow more than three marks per student (simply modify MaxMarks) and can be expanded to more than 10 students (by changing MaxStudents).
#include <iostream.h> #include <string.h> #include <tchar.h> #include <stdlib.h>
void main(void) { const int MaxStudents = 10; const int MaxMarks = 3; struct { double Mark[MaxMarks]; double Average; } StudentList[MaxStudents];
int i, j; // Initialise the student list for (i = 0; i < MaxStudents; i++) { for (j = 0; j < MaxMarks; j++) StudentList[i].Mark[j] = 0.0; StudentList[i].Average = 0.0; } // Assume a <space> will separate the marks char *separtors = " ";
// Now allow the user to enter the student marks char MarksInput[100]; int StudentCount = 0; int MarkCount = 0; do { cout<<"\r\nEnter Student #"<<StudentCount+1<<" data - "; cin.getline(MarksInput, sizeof(MarksInput) / sizeof(char)); char *token = strtok(MarksInput, separtors); MarkCount = 0; while ((token != NULL) && (MarkCount < MaxMarks)) { StudentList[StudentCount].Mark[MarkCount] = atof(token); MarkCount++; token = strtok(NULL, separtors); } if (MarkCount > 0) // Only increment the student count if it is valid StudentCount++; } while (MarkCount && (StudentCount < MaxStudents));
// The user has completed entering the student marks if (StudentCount > 0) { // Calculate the average for each student and the total average double TotalAverage = 0.0; for (i = 0; i < StudentCount; i++) { for (j = 0; j < MaxMarks; j++) StudentList[i].Average += StudentList[i].Mark[j]; TotalAverage += StudentList[i].Average; StudentList[i].Average /= MaxMarks; } TotalAverage /= (StudentCount * MaxMarks);
// Now display the results cout<<"Student Average is:"<<TotalAverage<<" for "<<StudentCount<<" Students\r\n"; for (i = 0; i < StudentCount; i++) { cout<<"Student #"<<i+1; for (j = 0; j < MaxMarks; j++) cout<<"\t"<<StudentList[i].Mark[j]; cout<<"\t"<<StudentList[i].Average<<"\t"; if (StudentList[i].Average < TotalAverage) cout<<TotalAverage-StudentList[i].Average<<" below"; else if (StudentList[i].Average > TotalAverage) cout<<StudentList[i].Average-TotalAverage<<" above"; cout<<" the average\r\n"; } } }
|
|