Reputation: 395
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
Upvotes: 1
Views: 421
Reputation: 12683
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
Upvotes: 1
Reputation: 41113
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
}
...
Upvotes: 1
Reputation: 7610
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;
}
}
}
Upvotes: 1