[C++] What's wrong with my quiz answer

Discussion in 'Computer Programming, Emulation, and Game Modding' started by Nyap, May 31, 2016.

  1. Nyap
    OP

    Nyap HTML Noob

    Banned
    973
    344
    Jan 13, 2016
    That Chaos Site
    so I'm doing a test on chapter 6 of my c++ tutorial and one of the questions is this:
    What's wrong with my answer (I know I should have used std::vector but I rushed the lesson on it because I just want to get out of this hellish chapter as soon as possible :cry:)?
    http://pastebin.com/amSCY7ja

    output:
     
  2. Hikari06

    Hikari06 GBAtemp Advanced Fan

    Member
    861
    611
    Nov 20, 2012
    France
    Well I don't know c++ very well but I would write something like this instead to begin:

    1. for (int k; k<numberOfStudents; k++)
    2. {
    3. cin >> students[k].name;
    4. cin >> students[k].grade;
    5. }
     
    Nyap likes this.
  3. btoast777

    btoast777 Newbie

    Newcomer
    8
    2
    Dec 31, 2013
    United States
    Well, right off the bat I see that in your main's for loop:

    You set the student's name twice. That's why your students' names are always their grade number. Line 4 here should be "cin >> students[student].grade."
     
    Nyap likes this.
  4. Nyap
    OP

    Nyap HTML Noob

    Banned
    973
    344
    Jan 13, 2016
    That Chaos Site
    yeah i just noticed that
    but the sorting algorithm doesn't work
     
  5. Wellington2k

    Wellington2k DO YOU HAS?

    Member
    760
    364
    Nov 1, 2010
    United States
    Somewhere in this world of ours
    Why do you use "int start{}" and "++start" in your for loop? I've taken two college level programming classes in C++ and we never used this.
    For a for loop, I do this:
    for (int i=0; i<count; i++) {
    //stuff here
    }

    EDIT: Also, where is your swap function?
    EDIT2: Okay, so swap is actually a built-in function. Learn something new everyday. :P
     
    Last edited by Wellington2k, May 31, 2016
    codeluca likes this.
  6. btoast777

    btoast777 Newbie

    Newcomer
    8
    2
    Dec 31, 2013
    United States
    Two things I have noticed in your sort method:

    First, on line 21, is that supposed to be a ">" sign? I'm a bit unfamiliar with the "start{}" expression, but I assume it sets "start" to 0. If that's the case then your loop will never run, since 0 will never be greater than the number of students you have!

    Also, you might have a problem when you up your list count to 3 or higher. On line 23 you declare the highestscore and higheststudent. Because this code comes after your first for loop, the next time it goes through the loop it's gonna set those back to zero. (This is known as an issue with "scope," a term you may or may not have not seen in your reading so far). You should instead move line 23 to before that first for loop.

    EDIT: Edited to add a little more info.
     
    Last edited by btoast777, May 31, 2016
  7. Nyap
    OP

    Nyap HTML Noob

    Banned
    973
    344
    Jan 13, 2016
    That Chaos Site
    {} is uniform initialization which was introduced in C++11
    my tutorial strongly reccomends using it because it help tell the difference between initialization and assignment, and a couple of other reasons aswell
    and I'm too lazy to explain the difference between using ++ as a prefix and as a postfix

    — Posts automatically merged - Please don't double post! —

    well I've made progress
    Code:
    void sortStudents(student *studentsToSort, int numberOfStudents)
    {
      for (int start{}; start<(numberOfStudents-1); ++start)
      {
      int highestScore{studentsToSort[start].grade}, highestStudent{start};
      for (int current{start+1}; current<numberOfStudents; ++current)
      {
      if ((studentsToSort[current].grade)>highestScore)
      {
      highestScore=studentsToSort[current].grade;
      highestStudent=current;
      }
      }
      swap(studentsToSort[start].grade, highestScore);
      swap(studentsToSort[start].name, studentsToSort[highestStudent].name);
      }
    }
     
  8. Wellington2k

    Wellington2k DO YOU HAS?

    Member
    760
    364
    Nov 1, 2010
    United States
    Somewhere in this world of ours
    Okay, interesting. I'm pretty sure we were purposefully not taught much C++ 11 to allow our code to work anywhere.
    Oh, and I know the difference between prefix and postfix, but I've never had to use prefix in a for loop.
     
  9. Nyap
    OP

    Nyap HTML Noob

    Banned
    973
    344
    Jan 13, 2016
    That Chaos Site
    I'm pretty sure it wouldn't make much of a difference used in a for loop like that

    — Posts automatically merged - Please don't double post! —
    Yeah, I used the wrong relational operator and I'm fully aware of scope

    — Posts automatically merged - Please don't double post! —

    I've fixed it! :D
    Code:
    void sortStudents(student *studentsToSort, int numberOfStudents)
    {
      for (int start{}; start<(numberOfStudents-1); ++start)
      {
      int highestScore{start}, highestStudent{start};
      for (int current{start+1}; current<numberOfStudents; ++current)
      {
      if ((studentsToSort[current].grade)>highestScore)
      {
      highestScore=current;
      highestStudent=current;
      }
      }
      swap(studentsToSort[start].grade, studentsToSort[highestScore].grade);
      swap(studentsToSort[start].name, studentsToSort[highestStudent].name);
      }
    }
    thnx for helping
     
    btoast777 likes this.