I'm currently working on a little project over winter break and have run into some issues.
Here is the struct that I'm working with:
struct student{
string last_name;
string first_name;
double exams[NUM_EXAMS];
double average;
char letter_grade;
bool passed;};
I am trying to alphabetize by last name from A to Z. Here is the alphabetize function as well as the swap function it calls:
void alphabetize(student class_list[], int count)
{
for (int pass = 0; pass < count; pass++)
for (int x = 0; x < count - pass; x++)
if (class_list[x].last_name < class_list[x + 1].last_name)
swap(class_list, x);
}
void swap(student class_list[], int x)
{
student temp[MAX_STUDENTS];
temp[x] = class_list[x];
class_list[x] = class_list[x + 1];
class_list[x + 1] = temp[x];
}
This runs perfectly fine and alphabetizes the array of structs in reverse order from Z to A.
Here is the original output unsorted:
Jones John 87 66 92 88 83.25 B Pass
Smith Peter 55 66 63 58 60.5 D Pass
Quest Nicole 79 89 99 98 91.25 A Pass
Wu Li 98 99 100 91 97 A Pass
West Vincent 80 80 88 89 84.25 B Pass
McCartin Susan 80 90 100 85 88.75 B Pass
Ibrahima Shuhuru 45 65 54 60 56 F Fail
Burns Antoinette 90 90 90 90 90 A Pass
Ng Lawrence 100 100 90 76 91.5 A Pass
Ziggler Bertha 65 55 58 58 59 F Fail
Ionella Jean 100 100 100 100 100 A Pass
Vogler Samuel 40 50 60 70 55 F Fail
Perry Jim 67 87 76 54 71 C Pass
and here is the output using
if (class_list[x].last_name < class_list[x + 1].last_name)
in the alphabetize function.
Ziggler Bertha 65 55 58 58 59 F Fail
Wu Li 98 99 100 91 97 A Pass
West Vincent 80 80 88 89 84.25 B Pass
Vogler Samuel 40 50 60 70 55 F Fail
Smith Peter 55 66 63 58 60.5 D Pass
Quest Nicole 79 89 99 98 91.25 A Pass
Perry Jim 67 87 76 54 71 C Pass
Ng Lawrence 100 100 90 76 91.5 A Pass
McCartin Susan 80 90 100 85 88.75 B Pass
Jones John 87 66 92 88 83.25 B Pass
Ionella Jean 100 100 100 100 100 A Pass
Ibrahima Shuhuru 45 65 54 60 56 F Fail
Burns Antoinette 90 90 90 90 90 A Pass
If I switch
if (class_list[x].last_name < class_list[x + 1].last_name)
in the alphabetize function to
if (class_list[x].last_name > class_list[x + 1].last_name)
I thought it would solve the problem and sort the array from A to Z rather than Z to A. This is what I get as an output:
-6.27744e+066-6.27744e+066-6.27744e+066-6.27744e+066-6.2
7744e+066 ═ Pass
Burns Antoinette 90 90 90 90 90 A Pass
Ibrahima Shuhuru 45 65 54 60 56 F Fail
Ionella Jean 100 100 100 100 100 A Pass
Jones John 87 66 92 88 83.25 B Pass
McCartin Susan 80 90 100 85 88.75 B Pass
Ng Lawrence 100 100 90 76 91.5 A Pass
Perry Jim 67 87 76 54 71 C Pass
Quest Nicole 79 89 99 98 91.25 A Pass
Smith Peter 55 66 63 58 60.5 D Pass
Vogler Samuel 40 50 60 70 55 F Fail
West Vincent 80 80 88 89 84.25 B Pass
Wu Li 98 99 100 91 97 A Pass
As you can see, I'm now missing what would be the last student in this list and instead the output is spitting out these numbers. I don't understand why it is working in reverse and I'm unsure of how to fix this. Any advice would be greatly appreciated!
EDIT: Thanks to Jarod42 I worked out a solution to my issue. It was an out of bounds issue with x + 1. Here is the code I used to fix the problem. It works with the input file that I have, but I'm unsure if it would work with a different one. If someone sees a problem with it please let me know.
void alphabetize(student class_list[], int count)
{
for (int pass = 0; pass < count; pass++)
for (int x = 0; x < count - pass; x++)
if (class_list[x].last_name > class_list[x + 1].last_name)
if (count > x + 1)
swap(class_list, x);
}
std::string
comparison?