Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

cn:Pls help for this programing problem in c++,better with some comment about wh

ID: 3914998 • Letter: C

Question

cn:Pls help for this programing problem in c++,better with some comment about why coding that way,thanks

Problem A: Counting Numbers Problem Description Write a program that reads numbers from the keyboard into an integer array. You may assume that there will be 50 or fewer entries in the array. Your program allows any number of numbers to be entered, up to 50 numbers. The output is to be a two-column list. The first column is a list of the distinct array elements; the second column is the count of the number of occurrences of each element. The list should be sorted on entries in the first column, largest to smallest. Input The first line contains an integer m which indicates the number of test cases. And the following is the m test cases. For each test cases, user can key in n number (n

Explanation / Answer

#include <iostream>

using namespace std;

int main() {

// TAKING the number of test cases

int n;

cin >> n;

// looping for that many times

for(int x=0; x<n; x++)

{

int nums[50];

int index = 0;

int now = 0;

// taking user input as long as numbers are not 50 and the number is not -999

while(index<50)

{

cin >> now;

if(now != -999)

{

nums[index++] = now;

}

else

{

break;

}

}

// diff is to hold different numbers

// count is to track the frequency of above numbers

int diff[50];

int count[50]={0};

// track is to track the number of unique elements

int track=0, i, j;

for(i=0; i<index; i++)

{

// if the current number is already in diff, incrementing it's count by 1

for(j=0; j<track; j++)

{

if(diff[j] == nums[i])

{

count[j]++;

break;

}

}

// if it is not in, then putting it in diff and making count 1

if(j==track)

{

diff[track] = nums[i];

count[track++] = 1;

}

}

// sorting the diff array in parallel with count in descending order

for(i=0; i<track; i++)

{

for(j=0; j<i; j++)

{

if(diff[i] > diff[j])

{

int temp = diff[i];

diff[i] = diff[j];

diff[j] = temp;

temp = count[i];

count[i] = count[j];

count[j] = temp;

}

}

}

// printing output

for(i=0; i<track; i++)

{

cout << diff[i] << " " << count[i] << endl;

}

cout << endl;

}

}

/*SAMPLE INPUTS AND OUTPUTS

2

1 2 3 1 2 1 4 -999

4 1

3 1

2 2

1 3

-12 3 -12 4 1 1

-12 1 -1 1 2 3 4 2 3 -12 -999

4 2

3 3

2 2

1 4

-1 1

-12 4

*/