×

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

Related Topics

C++ vs C#

What exactly is C++ programming? Bjorne Stroustrup is the creator of the C++ programming language. His goal was to create a powerful object-oriented programming language with the capabilities of C. It...

4 minutes read.

Parameterize Constructor

C++ Parameterized Constructor A constructor having parameters is known as parameterize constructor. Parameterize constructor is used to assign different values. Syntax: className(data-type argument){   // Constructor definition   }   className(data-type argument, data-type argument){   // Constructor definition   } A parameterized constructor can be passed values to constructor function in two ways: 1)...

1 minute read.

C++ Program to find the product array puzzle

Write a C++ program to form a product array from arr[] where product[i] is the product of all the array elements except arr[i]. Example Input: arr[]  = {10, 3, 5, 6,...

6 minutes read.

Upcasting and Downcasting in C++

With the help of various examples in the C++ programming language, this section will cover Upcasting and Downcasting. Upcasting and downcasting, on the other hand, are two forms of object typecasting. Consider...

3 minutes read.

Types of polymorphism in C++

What is polymorphism in C++ ? Polymorphism literally translates to "multiple forms". This indicates that the same thing behaves differently depending on the context in programming. Polymorphism is a feature of C++...

6 minutes read.

C++ int into String

Data type conversion is a standard editing process. You may need to convert variable from one type of data to another in a variety of situations. There are two ways...

5 minutes read.

Hashing in C++

Before understanding hashing, we need to know what the use of hashing is. Let us consider an example of a library which consists of many books.Having many books, searching for...

6 minutes read.

Bitwise Operator vs Logical Operator

Bitwise Operator  Bitwise operators perform operations bit by bit on bits.The value is converted to abinary during operations like addition, subtraction, division, and so on. These operations are carried out at the...

3 minutes read.

List back () function in C++ STL

The list::back () function of the C++ STL returns a direct reference to the last element in the list container. This function varies from list::end (), which just returns an...

2 minutes read.

fread() Function in C++ Programming

C++ language is used to make high-performance applications that can work efficiently, and it is one of the world's most popular languages. It is an object-oriented and high-level programming language;...

3 minutes read.

Message Passing in C++

The act of sending and receiving information by an object is referred to as communication, and all communication between objects that takes place via message is known as message passing....

1 minute read.

C++ Static

What is the Static keyword? In C++, the keyword static is used to give an element some particular properties. Static elements are only given storage in the static storage region once...

4 minutes read.

Difference between C and C++

What do you mean by C? C is a machine-independent structure or procedural oriented computer language that is widely utilized in a variety of applications. C is a fundamental programming language...

4 minutes read.

C++ Encapsulation

The following two essential components are present in all C++ programmes: Functions are the parts of a programme that perform actions, and they are termed programme statements (code).Program data is the...

4 minutes read.

Array program in C++

What is an Array? An array is a set of identically typed elements that are organized into contiguous memory locations and each element can be independently accessed using an index. We can...

16 minutes read.

Call by Pointer in C++

What is Pointer? Every variable in C++ has a specific address or location in the computer's memory, and this address is known as the memory address. A pointer can be defined...

5 minutes read.

Constructor Overloading in C++

A Constructor is a class member function that is used to initialize the class's objects. Constructors have no return type and are called automatically when an object is formed. Constructor Characteristics Constructors...

3 minutes read.

Octal to Decimal in C++

We need to write a system that converts octal number into equal decimal number when octal number is given as input. Let us look at an example of a program in...

2 minutes read.

Malloc() and new in C++

In C ++, malloc () and new are used for the same thing. During runtime, they are used to allocate memory. Malloc () and the new, on the other hand,...

4 minutes read.

C++ Heap Sort

Heapsort is executed on the structure of the heap data. We know heap is a complete tree in binary form. The heap tree can be of two different types: Min-heap,...

3 minutes read.