write a C++ program that outputs each number and its rank in the list from a giv
ID: 3665392 • Letter: W
Question
write a C++ program that outputs each number and its rank in the list from a given list of 20 distinct integers. The rank of a number x in list L is the number of elements larger than x in L.For example,
Input: 20 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1718 19 1
Output: (20 0) (2 18) (3 17) (4 16) (5 15) (6 14) (7 13) (8 12) (9 11) (10 10) (11 9) (12 8) (13 7) (14 6) (15 5) (16 4) (17 3) (18 2) (19 1) (1 19)
/* the order of the pairs in the list in the output is not important */
Your program must implement the following algorithm:
Read input of N=20 integers into array A
Scan array A as index i runs from 0 to N-1
calculate the rank of A[i] by counting all numbers that are larger than A[i]
Output all elements and their ranks in list L (i.e. array A)
In this assignment you are asked to implement list L as a linear linked list such that each node in L contains an element and its rank in array A, and next fields. write a C++ program that outputs each number and its rank in the list from a given list of 20 distinct integers. The rank of a number x in list L is the number of elements larger than x in L.
For example,
Input: 20 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1718 19 1
Output: (20 0) (2 18) (3 17) (4 16) (5 15) (6 14) (7 13) (8 12) (9 11) (10 10) (11 9) (12 8) (13 7) (14 6) (15 5) (16 4) (17 3) (18 2) (19 1) (1 19)
/* the order of the pairs in the list in the output is not important */
Your program must implement the following algorithm:
Read input of N=20 integers into array A
Scan array A as index i runs from 0 to N-1
calculate the rank of A[i] by counting all numbers that are larger than A[i]
Output all elements and their ranks in list L (i.e. array A)
In this assignment you are asked to implement list L as a linear linked list such that each node in L contains an element and its rank in array A, and next fields. write a C++ program that outputs each number and its rank in the list from a given list of 20 distinct integers. The rank of a number x in list L is the number of elements larger than x in L.
For example,
Input: 20 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1718 19 1
Output: (20 0) (2 18) (3 17) (4 16) (5 15) (6 14) (7 13) (8 12) (9 11) (10 10) (11 9) (12 8) (13 7) (14 6) (15 5) (16 4) (17 3) (18 2) (19 1) (1 19)
/* the order of the pairs in the list in the output is not important */
Your program must implement the following algorithm:
Read input of N=20 integers into array A
Scan array A as index i runs from 0 to N-1
calculate the rank of A[i] by counting all numbers that are larger than A[i]
Output all elements and their ranks in list L (i.e. array A)
In this assignment you are asked to implement list L as a linear linked list such that each node in L contains an element and its rank in array A, and next fields.
Explanation / Answer
Answer:
#include <iostream>
using namespace std;
int main()
{
const int n= 20;
int A[n] = {20,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,1};
cout << "Output :" << endl;
for(int i = 0; i < n; i++)
{
//cout << "Array[" << i << "] => " << A[i] << endl;
cout << "(" << A[i]<<","<<n-A[i]<<")";
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.