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 8952625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:54:57+00:00 2026-06-15T13:54:57+00:00

I am trying to sort an array of int and char s (from a

  • 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];
  • 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-06-15T13:54:59+00:00Added an answer on June 15, 2026 at 1:54 pm

    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

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

Sidebar

Related Questions

I'm trying to sort an array, filled with objects from a class i wrote.
I'm trying to sort an array in descending order in Java using this code:
I am trying to sort a two-dimensional array by a timestamp column Descending. This
I am trying to use qsort from STL to sort array of edge: struct
I'm trying to sort an array made of random numbers from 1 to 10
I am trying to sort a struct run array called results by a char,
So hello! I'm trying to sort int array with several threads. Now I've got
Problem : Trying to sort an array coming from a typedef struct I created
I'm trying to sort an array whose elements are read from a file which
I am trying to sort array with usort, but it doesn't work, What am

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.