Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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 p, then: (increase r by 1) return r
Example
Let us see the following implementation to get better understanding −
#includeusing namespace std; int solve(vector > 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 p) r++; } return r; } int main(){ vector > table = { { 100, 98, 100, 100 }, { 100, 100, 100, 100 }, { 90, 99, 90, 100 }, { 100, 98, 60, 99 } }; cout Input
{ { 100, 98, 100, 100 }, { 100, 100, 100, 100 }, { 90, 99, 90, 100 }, { 100, 98, 60, 99 } }Output
2
Advertisements
