1

I have 2 arrays one array consists students and second array consists random grades generated for these students. How can I write these arrays so I can use bubble sort to sort these arrays in descending order of grades?

students[i] = i # 0f students 
Grade1[i] = rand()%91+10 ; // score for exam 1,I did not write the loop which generates random numbers

How can I put these two together So i can sort by referencing grades.. and going from highest to lowest grades

4
  • Suggestion: Simplify the question by removing all references to the randomized data sources. You would like to combine two arrays into one for the purpose of sorting the values of one, using the values of the other as key. For that it doesn't matter whether the contents of the arrays are random. Commented Jun 14, 2013 at 3:50
  • ... Guess the homework is due?! Commented Jun 14, 2013 at 3:57
  • 1
    @BaidNation Your edit pretty dramatically changed the meaning of your question - as this version stands, there's nothing to suggest you want to sort one vector by the values of the other (as was originally the case). I think you may want to revisit it and revise again. Commented Jun 14, 2013 at 3:58
  • I edited my question, please take a look again. Thank you. Commented Jun 14, 2013 at 4:03

3 Answers 3

1

I suppose you want to put 2 single-Dimensional arrays with N elements each into a 2D array with N rows and 2 Columns, then the following code might be useful.

int arr[100][2];

cout<<"\n Enter the Number of students: ";
cin>>n;

for(int i=0; i<n; i++)
{
  arr[i][0]=students[i];
  arr[i][1]=Grade1[i];
}

// Now use bubble sort to sort the second column of array arr

for(int i=0; i<n; i++)
{
   for(int j=i; j<n; j++)
   {
      if(arr[i][1] < arr[j][1])
      {
         int t1;
         t1=arr[i][1];
         arr[i][1]=arr[j][1];
         arr[j][1]=t1;
         t1=arr[i][0];
         arr[i][0]=arr[j][0];
         arr[j][0]=t1;

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

Comments

1

Start with a simple implementation of bubble sort (eg: take a look at pseudocode available on Wikipedia), and then use student grade to compare ordering

Your can store student and grade in array like this. n-th element of grade array represent grade for n-th student

int studentIds[10];
int grade[10];

And on your bubble sort, use condition similar like following to compare student based on his/her grade

// iterate students using two indices i & j
...
if(grade[i] < grade[j]) {
  // .. bubble element i to the right
}
...

Comments

1

If you can use modern c++ tools like std::vector and other cool business like std::sort (which sorts an std::vector), it'd be much cleaner than doing it yourself with raw arrays. Learning to use container classes and the built-in algorithms of the language will serve you well.

#include <vector>
#include <algorithm>
#include <iostream>
struct Student //structure to hold student ID and grade info
{
    Student(int id, int grade):id(id),grade(grade){}
    int id;
    int grade;
};
//comparison operator to use with std::sort
bool operator <(const Student& lhs, const Student& rhs)
{
    return lhs.grade < rhs.grade;
}
int main()
{
    //You are starting with raw arrays...
    int studentID[5]{1,2,3,4,5};
    int grade[5]{90,91,73,62,87};

    //convert to std::vector of Student objects
    //not safe indexing into raw array, just an example
    std::vector<Student> students;
    for(unsigned int i=0;i<5;++i){
        students.push_back(Student(studentID[i],grade[i]));}

    //sort the vector (using the less-than operator provided above)
    std::sort(students.begin(),students.end());
    //reverse the vector, making it high-to-low order
    std::reverse(students.begin(),students.end());

    //print the result (uses std::for_each with a lambda function)
    std::for_each(students.begin(),students.end(),
    [](Student s){std::cout<<"ID: "<<s.id<<" grade: "<<s.grade<<std::endl;});
}

Output:

ID: 2 grade: 91
ID: 1 grade: 90
ID: 5 grade: 87
ID: 3 grade: 73
ID: 4 grade: 62

3 Comments

what if u had an array generated like this.. studentid = rand()% 31+131;
and grades in the same respect between 10 to 100;
@BaidNation Nothing would change - just replace the lines of code where studentID and grade are defined with your own code. Your question indicates that you already have this code...

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.