In: Computer Science
Write a C++ program that outputs a histogram of student Marks for a Mid-term-1 Examination. The program should input each student’s Marks as an integer and store the Marks in a dynamic array. Marks should be entered until the user enters -1 for marks. The program should then scan through the Dynamic array and compute the histogram. In computing the histogram, the minimum value of a marks is 0 but your program should determine the maximum value entered by the user. Then use a dynamic array to store and output the histogram. (20 points) For example, if the input is: 80 60 80 70 60 50 50 50 -1 Then the output should be: The frequency of 80's: 2 The frequency of 70's: 1 The frequency of 60's: 2 The frequency of 50's: 3
note: the arrays should be dynamic . furthermore the logic should not match other's logic, it should not be copy paste
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
int main()
{
///Initilaze vector To get user input dynamically
std::vector<int> myvector;
//Getting User Inputs till user enters -1
while (1) {
int myint;
std::cout << "Please enter some value (enter -1 to end):\n";
std::cin >> myint;
if(myint == -1){
break;
}
myvector.push_back(myint);
}
//Getting the size of user Inputs
int n = myvector.size();
int i;
// Initialise a set
// to store the array values
set<int> s;
// Insert the array elements
// into the set
for (i = 0; i < n; i++) {
// insert into set
s.insert(myvector[i]);
}
set<int>::iterator it;
// Print the duplicates count
for (it = s.begin(); it != s.end(); ++it){
int count = 0;
for( i = 0; i < n;i++ ){
if( myvector[i] == *it ){
count++;
}
}
cout << "The Frequency of " << *it <<"'s: " << count << "\n";
}
return 0;
}
Output: