×

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++ this pointer

'this' is a pointer that points to the object for which this function was called. The 'this' pointer holds the memory address of the current object. The 'this' pointer is implicitly passed to...

2 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.

External merge sort in C++

External merge sort in C++ External sorting is a concept for a group of sorting algorithms capable of handling large data volumes. External sorting is needed if the information getting sorted...

9 minutes read.

Star pattern in C++ using For Loops

Star patterns are one of the most extensively utilized patterns in any programming language since they help to increase logical thinking and flow control understanding.In the C++ programming language, you...

3 minutes read.

While Loop Examples in C++

While Loop A while loop repeats all code in its body, also known as a while statement, as long as a specific condition is satisfied. The loop ends if or when...

4 minutes read.

C++ File Handling

File handling is a mechanism that manipulates the data stored in files. File handling store output data from the program to external file and read file data to the program. There...

3 minutes read.

C++ Scope of Variables

In this tutorial, we will explore about the scope of variables in c++ programming language. And also, how it works in a program. What is Scope? The range of applications for something...

4 minutes read.

C++ If-else-if

Introduction: If-else-if control statement is an if statement used with an optional else if control statement to check multiple conditions. In this control statement, when any one of the condition returns...

4 minutes read.

Dynamic Memory Allocation in C++

In some programming situations, the number of data items changes as the program is running, which is known as dynamic data or input. Consider a real-world situation where a program...

3 minutes read.

C++ Dijkstra Algorithm Using the Priority Queue

In this article we will be finding the shortest routes from a source vertex in a graph to all vertices in the graph, given a graph and a source vertex...

5 minutes read.

Armstrong Number using While Loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

4 minutes read.

Reverse an Array in C++

The many approaches to reverse an array in the C++ programming language will be discussed in this section. The term "reverse of an array" refers to changing the order of...

8 minutes read.

Palindrome Using While Loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

Vector Size in C++

What are Vectors?  In the C++ programming language, vectors are run-time sequence containers representing arrays with variable sizes, which are contained within STL (Standard Template Library). They utilize contiguous storage spaces...

6 minutes read.

C++ Features

C++ is a general-purpose programming language that evolved from the C language to include an object-oriented paradigm. It is a compiled and imperative language. Object-Oriented Programming Object-oriented programming language concepts: ClassObjectsEncapsulationPolymorphismInheritanceAbstraction Class: A Class...

4 minutes read.

Nullptr in C++

What is Nullptr in C++? A null pointer value is represented by the term nullptr. Use a null pointer value to indicate that a native pointer type, inner pointer, or object...

3 minutes read.

C++ Program For FCFS (First Come First Serve)

The most basic scheduling technique is FCFS, often known as "FIFO (First In, First Out)". In this procedure, the first one is utilized and executed first, while the second one...

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.

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.

Memset in C++

Memset () is a function in the C++ programming language that fills memory blocks. The value of "ch" is first converted to an unsigned character. In this case, "ch" denotes the...

3 minutes read.