
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- 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?
- C++ code to find score of winning square on a square board
- Find minimum score from the entire four columns of a table in MySQL
- MongoDB aggregation / math operation to sum score of a specific student
- How to avoid null result of “SELECT max(rank) FROM test” for an empty table?
- Python program to find word score from list of words
- C++ code to count years to reach certain rank in an army
- Python program to find average score of each students from dictionary of scores
- How to find the rank of a vector elements in R from largest to smallest?
- Program to find maximum score from removing stones in Python
- Program to find maximum score from performing multiplication operations in Python
- Program to find the final ranking of teams in order from highest to lowest rank in python
- Change the column name from a MySQL table with Student record?
- How to find the rank of a matrix in R?

Advertisements