#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<fstream>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;
bool comparator( const std::pair<int, int> &left, const std::pair<int, int> &right )
{
return ( left.first > right.first);
}
void CalculateFrequency(int *arr , int n )
{
vector< pair<int,int> > moves;
pair<int,int> position;
for(int i=0;i<n;++i){ //iterate through array
bool flag= false; //check if encounter duplicates
size_t x=0;
for ( ; x < moves.size(); ++x ) { //iterate the vector
if(moves[x].first == arr[i]){
moves[x].second++;
flag = true;
break;
}
}
if(!flag){
position.first = arr[i];
position.second = 1;
moves.push_back(std::make_pair(position.first, position.second)); //Make pair i.e first element as array element and 2nd element as Count
}
}
sort(moves.begin() , moves.end(), comparator); //Sort the pair in descending order using comparator
cout << " N" <<" " << "Count" << endl;
for ( size_t x = 0; x < moves.size(); ++x ) { //Print Output
printf(" %d\t%d\n",moves[x].first ,moves[x].second);
}
}
int main ()
{
char choice;
while(true ) {
printf("Enter Your choice\n" ) ;
printf("1. Take input from the Keyboard\n");
printf("2. Take Input from the File\n");
printf("3. Exit\n" ) ;
scanf("%c",&choice);
string filename;
int n, count=0;
int *arr;
ifstream File;
switch(choice)
{
case '1' :
printf("Enter the number of elements\n");
scanf("%d",&n);
arr = (int *)malloc(n*sizeof(int));
for(int i=0;i<n;++i)
{
if(i==0)
printf("Enter %dst element: ", (i+1) );
else if(i==1)
printf("Enter %dnd element: ", (i+1) );
else if(i==2)
printf("Enter %drd element: ", (i+1) );
else
printf("Enter %dth element: ", (i+1) );
scanf("%d",&arr[i]);
}
CalculateFrequency(arr, n);
break;
case '2':
printf("Enter the name of file\n");
cin>>filename;
int a;
File.open(filename);
while (File >> a){
++count;
}
File.close();
File.open(filename);
arr = (int *)malloc(count*sizeof(int));
for(int a = 0; a < count; a++)
File >> arr[a];
CalculateFrequency(arr, count);
break;
case '3':
exit(0);
break;
default:
printf("Wrong Choice\n");
}
}
return 0;
}
=================================================================================
Adding Images, Comments and Output for more clarity
Output
Thanks, Let me know if there is
any concern. Please provide feedback.
Attaching more Images
MORE RUNS
Let me know your concern.
In C++ Sorted List of User Entered Numbers 2. Write a program that reads in a...
C++ Programming question Problem: 5. Write a program that reads in a list of integers into an array with base type int. Provide the facility to either read this array from the keyboard or from a file, at the user's option. If the user chooses file input, the program should request a file name. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is to be...
:Pls help for this programing problem in c++ (standard IO without file read/write),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...
In C++: Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The list is preceded by an integer indicating how many numbers are in the list. If the input is: 5 10 5 3 21 2, the output is: 2 3 To achieve the above, first read the integers into a vector.
Please and thank you 2) (5 pts) Arrays & Vectors: write a program which prompts the user to enter numbers (terminated with a non-numeric), reads them simultaneously into an array of doubles and into a vector of doubles, then prints A) the array elements in the original order B) the vector elements in reverse order C) the average of the vector elements D) the largest of the vector elements Example program output, user input shown underlined Enter a list of...
Write a complete C++ program to ask the user to enter 4 integers. The program must use a function to find the smallest integer among them and print it on the screen. Typical output screen should be as following: Write a complete C++ program to ask the user to enter 4 integers. The program must use a functionto find the smallest integer among them and print it on the screen. Typical output screen should be as following: Note: the code...
Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...
Write a program that reads integers, finds the smallest of them, and counts its occurrences. Assume that the input ends with number -1. Suppose that you entered 4 2 9 2 2 -1; the program finds that the smallest is 2 and the occurrence count for 2 is 3. (Hint: Maintain two variables, min and count. min stores the current min number, and count stores its occurrences. Initially, assign the first number to min and 1 to count. Compare each...
This is for C++ Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must...
IN PYTHONWrite a program that reads a list of integers in the range (0 – 49) and prints out the numbers that does not repeat twice.The first input of the program is the number of integers contained in this list, followed by the integers contained in the list. Print the numbers in in ascending order.Case 1:INPUT: 5 2 2 3 3 1OUTPUT:1Case 2:INPUT:6 1 2 1 2 2 2OUTPUT:2Case 3:INPUT:4 3 2 1 2OUTPUT: 1 3343 2 1 213
Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...