The program is suppose to declare a structure of student record:
- Student name – string type
- Student grade1 – integer
- Student grade2 – integer
- Student grade3 – integer
- Average score – float
Use a loop to enter 10 student names and their 3 test scores
Calculate the average of each student
Sort the array into ascending order
Print the student record with the highest average
Print the student with the lowest average
This is the source code that I have created:
Code:
#include <iostream>
#define TAB '\t'
#define MAX 13
// Creating the structure/array
struct studentrec {
char name[15];
int test1, test2, test3;
float Ave;
};
main() {
struct studentrec testrec[MAX];
int i; // loop control
// Inputing data into the structure
for (i = 0; i < MAX; i++) {
cout << "Name" << endl;
cin >> testrec[i].name;
cin >> testrec[i].test1;
cin >> testrec[i].test2;
cin >> testrec[i].test3;
// Calculating the average of each student
testrec[i].Ave = (testrec[i].test1 + testrec[i].test2 + testrec[i].test3) / 3;
// Setting the structure
struct studentrec temp;
for (int k = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
if (testrec[i].Ave > testrec[j+1].Ave) {
temp = testrec [j];
testrec[j] = testrec [j+1];
testrec[j+1] = temp;
} // end of if (testrec[i].Ave > testrec [j+1]) statement
// Printing the structure
for (i+0; i < MAX; i++) {
cout << testrec[i].name
<< testrec[i].test1 << TAB
<< testrec[i].test2 << TAB
<< testrec[i].test3 << TAB
<< testrec[i].Ave;
} // end of for j loop
} // end of for i loop
} // end of for i+0 loop
} // end of for k loop
return (0);
} // end of main