Find Top-Ranked Student Based on Exam Scores in C++

By | November 25, 2024

There are N student from class 12th,each student finished thier exam and exam consist with four subjects like math, computer, physics and chemistory. Result is announced and result list consist with student identity number with their corresponding marks scored in each subject. Our task is to find ranked student among the N student.

Example:
Suppose there are 5 number of students and they had unique identity number and the result board shown as below.

Studentid math comp phy chem -
1 100 99 98 96
2 100 100 100 99
3 92 95 91 95
4 100 92 94 91
5 90 100 95 93 

From the above result board student number 2 scrored max from all 5 student therefor output must be 2.

Explaination:

Find the total score of each student
stud1 total score = 100+99+97+96 = 392
stud2 total score =100+100+100+99 = 399
stud3 total score = 92+95+91+95 = 373
stud4 total score = 100+92+94+91 = 377
stud5 total score = 90+100+95+93 = 378 

From the above result student no.2 score the highest marks so the result is 2.

Example:

stuId math comp phy chem
1 100 100 99 100
2 70 75 70 65
3 80 68 50 60 

Form the above result board student with I’d 1 score highest marks so that output must be 1.

Note:
If more than one student score same marks then print first student according to increasing order of their student id.

Approach:

  1. Initialise rank to zero and sum array to zero that stores sum of scores of each student.
  2. Traverse score matrix and calculate sum of each row.
  3. Travese a loop for each student check if current sum of score is greater then previous sum of score if so increament rank pointer to next finally return rank+1.

Using the above aproach Below is c++ program to find the rank student.

//C++ program to find a ranked student.
#include <iostream>
using namespace std;
 
int findRank(int a[1000][4],int n)
 {
   //array that stores the sum of
  //score each student.
     int sum[1000];
  //initialise rank to zero.
     int rank = 0;
     int i,j;
  //traverse the score matrix and
  //find the sum of score of each 
  //student.
   for(i = 0;i<n;i++)
     {
       for(j = 0;j<4;j++)
        {
           sum[i]+= a[i][j];
         }
      }
//for the each student 
//compare the score.
     for(i = 1;i<n;i++)
        {
        if(sum[i]>sum[0])
           {
            rank++;
           }
        }
       return rank+1;
}
int main()
  {
     //Score matrix.
  int a[1000][4] = {{100,99,98,96},
                   {100,100,100,99}, 
                   {92,95,91,95}, 
                   {100,92,94,91},
                   {90,100,95,93} };
//number of students
    int n = 5;
    cout << findRank(a, n);
     return 0;
}

Output:

 2

Student with id 2 score highest marks therefor output is 2.

Author: Mithlesh Upadhyay

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.