0

The code is designed to output a student roster, and enable the sorting of the table by first name, last name, grade, among others.

My program compiles and I actually get credit for the first few problems, however it seems I am having trouble with my SortByFirstName and SortByLastName functions, or the way those functions interact with SortByGrade. (these functions are at the bottom of the code, everything else should work fine)

When attempting to sort by last name or by first, the output comes out nearly correct, that is, the list appears to be sorted properly but then two of the middle names are unsorted. The grade values are also improperly swapped (or not swapped at all?).

When just sorting by grade there appears to be no problem, so Im assuming the error lies in the SortByFirstName and SortByLastName functions.

Heres an example error output for my current code (for SortByFirstName):

Expected: | | Found:

  • Alyx Masinas | | Alyx Masinas
  • Ginnie McDonald | | Nola Manon
  • Nola Manon | | Ginnie McDonald
  • Randy Rubin | | Randy Rubin
  • Sephora McDonald | | Sephora McDonald

Heres another (for SortByFirstName + SortByGrade):

Expected: | | Found:

  • Alyx Masinas | | Alyx Masinas
  • Ginnie McDonald | | Nola Manon
  • Nola Manon | | Ginnie McDonald
  • Randy Rubin | | Randy Rubin
  • Sephora McDonald | | Sephora McDonald

Expected: | | Found:

  • 83.5 | | 83.5
  • 98.7 | | 100
  • 100 | | 98.7
  • 89.5 | | 89.5
  • 100 | | 100

Why are my functions not sorting properly? After finally getting it to all compile I don't know where to go. Ive been looking in my notes for similar practice problems where the vector was not fully selected during a function call. Any help would be appreciated, thank you.

3_5
  • 1
  • 1
  • 2
    note the existence of [`std::swap`](https://en.cppreference.com/w/cpp/algorithm/swap) – JHBonarius Oct 28 '20 at 20:44
  • 4
    What does the debugger tell you your code is doing? – JHBonarius Oct 28 '20 at 20:46
  • 1
    are you not allowed to use `std::sort` ? – 463035818_is_not_a_number Oct 28 '20 at 20:47
  • 2
    Here's some additional info, regarding @JHBonarius comment: https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems https://stackoverflow.com/questions/12546706/good-debugger-tutorial-for-beginners – πάντα ῥεῖ Oct 28 '20 at 20:49
  • Well the first set of menu options works fine. When attempting to sort the list, by first name, for example out of 5 names the 2nd and 3rd names are incorrectly not swapped. When sorting by first name and with grades, the same error is apparent, the 2nd and 3rd name/grade on the list do not sort properly (they should be switched). – 3_5 Oct 28 '20 at 20:56
  • I believe we are limited to the scope of selection sort, thats why I haven't wanted to just take an answer from online.. wait hol' up. – 3_5 Oct 28 '20 at 20:58
  • Read this [excellent advice on how to find the problem](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – paddy Oct 28 '20 at 21:07
  • no I dont think we can use std:sort – 3_5 Oct 28 '20 at 21:08

1 Answers1

1

In your sort functions' inner loops, you're not taking into account that when you swap two students (e.g. when firstName2 moves into the slot formerly occupied by firstName1) you now need to compare subsequent elements against the new smallest-known entry (firstName2 in this case).

Add one line:

if (firstName1 > firstName2) {
    temp = students[i];
    students[i] = students[j];
    students[j] = temp;
    tempGrade = grades[i];
    grades[i] = grades[j];
    grades[j] = tempGrade;

    // now only look for items that should come before our new "leader"
    firstName1 = firstName2;
}
Paul Roub
  • 35,848
  • 27
  • 79
  • 88