My program is supposed to accept the names of students with one test score. Then is supposed to organize the students in descending order based on their scores. It also gives off the average score.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include <iostream>
#include <string>
using namespace std;
//Student class
class student{
public:
string firstname;
string lastname;
float testscore;
student operator=(const student &right){
student opstu;
opstu.firstname = right.firstname;
opstu.lastname = right.lastname;
opstu.testscore = right.testscore;
return opstu;
}
};
int main(){
cout << "Enter number of students:";
int numstudent, avgscore = 0;
student *pstudent, temp;
bool swapp = false;
cin >> numstudent;
pstudent = new student[numstudent];
//Accepts info of students
for(int index = 0; index < numstudent; index++){
cout << "Info for student #" << index+1 << endl;
cout << "Enter student first name: ";
cin >> pstudent[index].firstname;
cout << "Enter student last name: ";
cin >> pstudent[index].lastname;
cout << "Enter student test score: ";
cin >> pstudent[index].testscore;
avgscore += pstudent[index].testscore;
}
//The sort
do
{swapp = false;
for(int pass =0; pass < numstudent-1; pass++){
if(pstudent[pass].testscore > pstudent[pass+1].testscore){
temp = pstudent[pass];
pstudent[pass] = pstudent[pass+1];
pstudent[pass+1] = temp;
}}}while(swapp);
cout << "Results" << endl;
//Prints results
for(int n = 0; n < numstudent; n++){
cout << "Name: " << pstudent[n].firstname << " " << pstudent[n].lastname << endl;
cout << "Score: " << pstudent[n].testscore << endl;
}
cout << "Average score was " << avgscore/numstudent << endl;
return 0;
}
|
Its works pretty well, but the output does sort. Heres an example of the output:
Enter number of students:3
Info for student #1
Enter student first name: Chris
Enter student last name: Smith
Enter student test score: 25
Info for student #2
Enter student first name: John
Enter student last name: Baker
Enter student test score: 95
Info for student #3
Enter student first name: Bill
Enter student last name: Lopez
Enter student test score: 35
Results
Name: Chris Smith
Score: 25
Name: John Baker
Score: 95
Name: Bill Lopez
Score: 35
Average score was 51
Please and thanks in advance and sorry about the long post.