Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7581833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:17:26+00:00 2026-05-30T18:17:26+00:00

I am working through a C++ book, and I am a little stuck on

  • 0

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).

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.

Here is the function I have for the selection sort:

void selection_sort(Student *ptr, int size) // selection sort - having some problems
{
  int start,
    min_index,
    min_value;

  for (start = 0; start < (size - 1); start++) {
    min_index = start;
    min_value = (ptr+start)->score;
    for (int index = start+1; index < size; index++) {
      if ( (ptr+index)->score < min_value) {
    min_value = (ptr+index)->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)->score = min_value;
  }
}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:17:28+00:00Added an answer on May 30, 2026 at 6:17 pm

    First of all I would like to give you one tip: instead of using the syntax *(ptr+min_index) you can use ptr[min_index] and it will have the same effect. I believe this version is more natural.

    Second – your problem. You should swap ptr[min_index] and ptr[start] rather then just copying the values of one of them to the other.
    That is instead of:

    *(ptr+min_index) = *(ptr+start);
    (ptr+start)->score = min_value;
    

    Write this:

    Student temp = ptr[start];
    ptr[min_index] = ptr[start];
    ptr[start] = temp;
    

    Or if you are using c++ simply use the swap function:

    std::swap(ptr[min_index], ptr[start]);
    

    Why should you swap instead of what you are currently doing? Well, you should preserve all the fields in ptr[min_index] in order to be able to assign them to ptr[start].

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working through more book examples- this one is a partial poker program- This segment
I'm working through the 'Learning Cocos2d' book, and I'm stuck on something basic. So,
I'm working through the Grails in Action book, and I'm stuck at that part
I am working through Beginning iPhone Development . In the book is this method:
I am working through the book Learning OpenCV from the O'Reilly series and am
I'm working through the book The Definitive Guide to Django and am stuck on
I am working through the book Learning WCF and on the first tutorial lab
Having problems compiling this class. I'm working through a book on java and this
I'm slowly working through an Android learning book and was given the following code
I'm working through the book Beginning BlackBerry 7 Development and really putting my little

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.