Structure Sorting (By Multiple Rules) in C++
To understand the concept of Structure Sorting (By Multiple Rules) in C++, it is recommended to know the Structures concept in the C++ programming language. Here the scenario is pretty simple. We have t sort the details of five students using the idea of the structure.
Below is the structure template that we are going to use before we commence with coding:
struct Student .
{
string name; //it will be a given detail
int mathematics; //the Marks in math (Given)
int physics; //the Marks in Physics (Given)
int chemistry; //the Marks in Chemistry (Given)
int total marks; //the Total marks (To be filled)
int rank after sorting ; //display Rank of student (To be filled)
};
To understand the long lines of code, we will start by understanding different concepts of the C++ programming language we have used to build the logic in our program to display the details of the five student use cases in this scenario.
We have std::sort() function concept in C++ programming language. In this structure sorting, structure object qualities or structure object properties that we have given to them will all be made applicable to objects sorted based on the property of that object.
C++ code to demonstrate Structure Sorting (By multiple Rules) in C++
Code
//Here we are writing the code in the
//C++ program to demonstrate Structure Sorting in C++
#include <bits/stdc++.h> //initializing the library
using namespace std; //utilizing the namespace std
struct Student_details
{
string name; //it is a given detail
int mathematics; //the marks in mathematics also a (Given) detail
int physics; //the marks in physics also a (Given) detail
int chemistry; //the marks in chemistry also a (Given) detail
int total; //the marks in the total also a (Given) detail
int rank; //the marks in tital calculated for the rank a caluclted.
};
//down the codes are written for hunction for comparing two students //according to given rules
// function starts here
bool compare_Two_Students(Student_details A, Student_details B)
{
// If total marks are not same then
// returns true for higher total
if (A.total != B.total)
return A.total > B.total;
// If marks in Maths are same then
// returns true for higher marks
if (A.mathematics != B.mathematics)
return A.mathematics > B.mathematics;
if (A.physics != B.physics)
return A.physics > B.physics;
return (A.chemistry > B.chemistry);
}
// it will Fill total marks and ranks of all Students
void compute_Ranks(Student_details A[], int n)
{
// from here To calculate total marks for all Students
for (int i = 0; i < n; i++)
A[i].total = A[i].mathematics + A[i].physics + A[i].chemistry;
//this is the Sort structure array using user defined
//this is the function compareTwoStudents()
sort(A, A + 5, compare_Two_Students);
//from here Assigning ranks after sorting
for (int i = 0; i < n; i++)
A[i].rank = i + 1;
}
// this is the Driver code
int main()
{
int n = 5;
//here we have an array of structure objects
Student_details A[n];
// display details of Student 1
A[0].name = "helo";
A[0].mathematics = 180;
A[0].physics = 195;
A[0].chemistry = 385;
//display details of Student 2
A[1].name = "Peter";
A[1].mathematics = 395;
A[1].physics = 825;
A[1].chemistry = 299;
//display details of Student 3
A[2].name = "Galra";
A[2].mathematics = 395;
A[2].physics = 285;
A[2].chemistry = 380;
//display details of Student 4
A[3].name = "Jobs";
A[3].mathematics = 280;
A[3].physics = 270;
A[3].chemistry = 390;
//display details of Student 5
A[4].name = "Joshi";
A[4].mathematics = 380;
A[4].physics = 380;
A[4].chemistry = 380;
compute_Ranks(A, n);
//display column names for displaying data
cout << "Rank"
<< " "
<< "Name"
<< " ";
cout << "Maths"
<< " "
<< "Physics"
<< " "
<< "Chemistry";
cout << " "
<< "Total\n";
// Display details of Students from here
for (int i = 0; i < n; i++) {
cout << A[i].rank << " ";
cout << A[i].name << " ";
cout << A[i].mathematics << " " << A[i].physics << " "
<< A[i].chemistry << " ";
cout << A[i].total << " ";
cout << "\n"; }
return 0;}
Output
Rank Name Maths Physics Chemistry Total
1 Peter 395 825 299 1519
2 Joshi 380 380 380 1140
3 Galra 395 285 380 1060
4 Jobs 280 270 390 940
5 helo 180 195 385 760