0

I am trying to sort an array of int and chars (from a class) by descending order. These are student names and grades.

The class is defined as:

class Student {
public:
    char name[20];
    int grades;
};

numCount is the incremental value of number of records.

void bubble_sort(Student theResults[], int numCount)
{
  bool swapped = true;
  while(swapped)
  {
    swapped = false;
    for(int i=1;i<numCount;i++)
    {
      if(theResults[i-1].grades < theResults[i].grades)
      {
        int tempHold = theResults[i-1].grades;
        theResults[i-1].grades = theResults[i].grades;
        theResults[i].grades = tempHold;
        swapped = true;
      }
    }
  }

The issue I am having is that the int values (grades) are sorted correctly after the loop but having difficulty getting the names to be correctly allocated to match with the grades.

I have used the following code but it doesn't work as it displays the incorrect grades for the students.

char* title_temp = theResults[i-1].name;
theResults[i-1].name[20] = theResults[i].name[20];
theResults[i].name[20] = title_temp[20];

3 Answers 3

1

I think your problem is here:

if(theResults[i-1].grades < theResults[i].grades)
{
    int tempHold = theResults[i-1].grades;

    theResults[i-1].grades = theResults[i].grades;

    theResults[i].grades = tempHold;

    swapped = true;
}

What you really want to do is

if(theResults[i-1].grades < theResults[i].grades)
{
    Student tempHold = theResults[i-1];

    theResults[i-1] = theResults[i];

    theResults[i] = tempHold;

    swapped = true;
}

Before all you were changing was the grade value and not the names, this will switch the entire Student object and should produce the output you are looking for

Sign up to request clarification or add additional context in comments.

1 Comment

Siegester, thank you. The code worked perfectly. Thanks for your help.
1

You'd have to copy the entire char block, each element at a time using a loop, or you could use memcpy.

You could also use a shallow copy of your class

void bubble_sort(Student theResults[], int numCount)
{


    bool swapped = true;
    while(swapped)
    {
        swapped = false;
        for(int i=1;i<numCount;i++)
        {
            if(theResults[i-1].grades < theResults[i].grades)
            {
                Student tempHold = theResults[i-1];

                theResults[i-1]= theResults[i];

                theResults[i] = tempHold;

                swapped = true;
            }
        }
    }
}

1 Comment

Nico, The above code also worked perfectly. Thank you for your time and help.PS Sorry could only mark 1 as a correct answer and I did the first post even though all three posts were correct. Sorry.
1

The problem is that you need to swap the objects, the grades only have to act as a key to guide the sort, try this :

void bubble_sort(Student theResults[], int numCount)
{

    Student tempHold;
    bool swapped = true;
    while(swapped)
    {
        swapped = false;
        for(int i=1;i<numCount;i++)
        {
            if(theResults[i-1].grades < theResults[i].grades)
            {
                tempHold = theResults[i-1]; //swap the objects, not just the grades.

                theResults[i-1]= theResults[i];

                theResults[i] = tempHold;

                swapped = true;
            }
        }
    }}

However, if you must copy members, then in addition to swapping grades :

char temp[20];
strcpy(temp ,theResults[i-1].name);
strcpy(theResults[i-1].name,theResults[i].name);    
strcpy(theResults[i].name,temp);

Instead of using

    char* title_temp = theResults[i-1].name; // <-wrong
   theResults[i-1].name[20] = theResults[i].name[20];//20 is invalid index
    theResults[i].name[20] = title_temp[20]; //this is just 1 element out of the whole array

which is wrong due to many reasons.

1 Comment

axiom, excellent :D Also worked like a charm. Thank you for your help and advice. PS Sorry could only mark 1 as a correct answer and did the first post even though all three posts were correct. Sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.