Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Selection Sort on Array of Structures
    primarykey
    data
    text
    <p>I am working through a C++ book, and I am a little stuck on one of the challenge questions. I'm working on learning about pointers, and in this particular problem I need to sort an array of structures (using pointers) with a string of a student's name and a double of their score. After the sort, the structure's data members still need to match up, obviously (i.e. the right name's still need to be with their scores).</p> <p>This is where my problem lies. So far, I have the sort properly arranging the scores in ascending order, but the names get all jumbled up. I haven't been able to figure out why, partly because I am still working to fully understand pointers and how to use them. I can do a bubble sort correctly, keeping the names with their scores, but not the selection sort. Any help would be greatly appreciated.</p> <p>Here is the function I have for the selection sort:</p> <pre><code>void selection_sort(Student *ptr, int size) // selection sort - having some problems { int start, min_index, min_value; for (start = 0; start &lt; (size - 1); start++) { min_index = start; min_value = (ptr+start)-&gt;score; for (int index = start+1; index &lt; size; index++) { if ( (ptr+index)-&gt;score &lt; min_value) { min_value = (ptr+index)-&gt;score; min_index = index; } } // the following line is where, i think, the problem is, but i haven't // been able to figure out the solution, despite trying numerous approaches *(ptr+min_index) = *(ptr+start); (ptr+start)-&gt;score = min_value; } } </code></pre> <p>So that is what I have. I'm not great with sorting algorithms yet either, and this is all pretty new to me, so I hope it's not horribly messed up. If anyone knowledgeable in these areas could point me in the right direction, that would be awesome.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload