 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ code to find rank of student from score table
Suppose we have a 2d array of size n x 4. Consider there are n students and their ids are starting from 0 to n-1. Each of them has four scores on English, Geography, Maths and History. In the table, the students will be sorted by decreasing the sum of their scores. If two or more students have the same sum, these students will be sorted by increasing their ids. We have to find the id of student whose id is 0.
So, if the input is like
| 100 | 98 | 100 | 100 | 
| 100 | 100 | 100 | 100 | 
| 90 | 99 | 90 | 100 | 
| 100 | 98 | 60 | 99 | 
then the output will be 2
Steps
To solve this, we will follow these steps −
n := size of table r := 1 p := table[0, 0] + table[0, 1] + table[0, 2] + table[0, 3] for initialize i := 1, when i < n, update (increase i by 1), do: if table[i, 0] + table[i, 1] + table[i, 2] + table[i, 3] > p, then: (increase r by 1) return r
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(vector<vector<int>> table){    int n = table.size();    int r = 1;    int p = table[0][0] + table[0][1] + table[0][2] + table[0][3];    for (int i = 1; i < n; i++){       if (table[i][0] + table[i][1] + table[i][2] + table[i][3] > p)          r++;    }    return r; } int main(){    vector<vector<int>> table = { { 100, 98, 100, 100 }, { 100, 100, 100, 100 }, { 90, 99, 90, 100 }, { 100, 98, 60, 99 } };    cout << solve(table) << endl; } Input
{ { 100, 98, 100, 100 }, { 100, 100, 100, 100 }, { 90, 99, 90, 100 }, { 100, 98, 60, 99 } }  Output
2
Advertisements
 