- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- 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

- Related Questions & Answers
- C++ code to find maximum score we can assign to first student
- How to get the 2nd highest value from a table with Student Score?
- MongoDB aggregation / math operation to sum score of a specific student
- Find minimum score from the entire four columns of a table in MySQL
- C++ code to find score of winning square on a square board
- Python program to find word score from list of words
- Python program to find average score of each students from dictionary of scores
- Change the column name from a MySQL table with Student record?
- Program to find maximum score from removing stones in Python
- Group the marks of a particular student from a table and display total marks in a separate column for each student?
- How to find the rank of a vector elements in R from largest to smallest?
- Program to find maximum score from performing multiplication operations in Python
- How to avoid null result of “SELECT max(rank) FROM test” for an empty table?
- C++ code to count years to reach certain rank in an army
- How to find the rank of a matrix in R?

Advertisements