Mar 29, 2015 at 1:11am UTC
I'm having an issue figuring out how to make a bubble sort, sort by high school name. I am suppose to create a program where the user can enter student's names, their high school name and their grades for 2 exams and then sort the students by high school; however, i'm not exactly sure why my bubble sort isn't working. Below is the code I have so far. Any help is appreciated.
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#include <iostream>
#include <string>
using namespace std;
struct studentInfo{
string firstLast = "" ;
string first = "" ;
string last = "" ;
string highSchool;
int spaceLocation = 0;
double testOne,
testTwo, gradeMean;
};
int main()
{
studentInfo studentArray[150],
temp;
int maxSub;
int lastSwap = 0;
char swap = 'Y' ;
int x = 0;
int sub = 0;
cout << "Enter the students name ex.(John Doe): \n" ;
cout << "Type 'exit' when you are ready to exit: \n" ;
getline(cin, studentArray[x].firstLast);
studentArray[x].spaceLocation = studentArray[x].firstLast.find(" " , 0);
studentArray[x].first = studentArray[x].firstLast.substr(0, studentArray[x].spaceLocation);
studentArray[x].last = studentArray[x].firstLast.substr(studentArray[x].spaceLocation + 1);
//reading in the information
while (studentArray[x].firstLast != "exit" )
{
cout << "\nEnter " << studentArray[x].firstLast << "'s high school.\n" ;
getline(cin, studentArray[x].highSchool);
cout << "\nEnter " << studentArray[x].firstLast << "'s exam one score.\n" ;
cin >> studentArray[x].testOne;
cin.ignore();
cout << "\nEnter " << studentArray[x].firstLast << "'s exam two score.\n" ;
cin >> studentArray[x].testTwo;
cin.ignore();
//calculating the mean of the two test grades
studentArray[x].gradeMean = (studentArray[x].testOne + studentArray[x].testTwo) / 2;
x++;
cout << "--------------------------------------------\n" ;
cout << "Enter the students name ex.(John Doe): \n" ;
cout << "Type 'exit' when you are ready to exit: \n" ;
getline(cin, studentArray[x].firstLast);
studentArray[x].spaceLocation = studentArray[x].firstLast.find(" " , 0);
studentArray[x].first = studentArray[x].firstLast.substr(0, studentArray[x].spaceLocation);
studentArray[x].last = studentArray[x].firstLast.substr(studentArray[x].spaceLocation + 1);
}
//sorting the array
maxSub = x - 1;
while (swap == 'Y' )
{
swap = 'N' ;
sub = 0;
while (sub < maxSub)
{
if (studentArray[x].highSchool == studentArray[sub + 1].highSchool)
{
temp = studentArray[sub];
studentArray[sub] = studentArray[sub + 1];
studentArray[sub + 1] = temp;
swap = 'Y' ;
lastSwap = sub;
}
sub++;
}
maxSub = lastSwap;
}
//returns the information
cout << "Last Name\tFirst Name\tHighschool\t\t\tGrade" << endl;
cout << "---------\t----------\t----------\t\t\t-----" << endl;
for (int y = 0; y <= x - 1; y++)
{
cout << studentArray[y].last << "\t\t" ;
cout << studentArray[y].first << "\t\t" ;
cout << studentArray[y].highSchool << "\t\t" ;
cout << studentArray[y].gradeMean << "\t\t" << endl;
}
system("pause" );
return 0;
}
Last edited on Mar 29, 2015 at 1:25am UTC
Mar 29, 2015 at 7:35am UTC
Mistakes on line 70, 72, and 82. I think it should be obvious the problems with 72. The problem with 71, what is studentArray[sub + 1]
when sub == maxSub - 1
? As for line 82, I understand why you have a lastSwap
variable, but the way you're using it is wrong.