C++ Exercises: Reads a sequence of integers and prints mode values of the sequence
C++ Basic: Exercise-71 with Solution
Write a C++ program which reads a sequence of integers and prints mode values of the sequence. The number of integers is greater than or equals to 1 and less than or equals to 100.
Note: The mode of a set of data values is the value that appears most often.
Sample Solution:
C++ Code :
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums(101, 0);
int n, mode=0;
while (cin >> n) {
nums[n]++;
if (nums[n] > mode) { mode=nums[n]; }
}
for (int i = 0; i != nums.size(); ++i) {
if (nums[i] == mode)
{
cout << i << endl;
}
}
return 0;
}
Sample Output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
Flowchart:
C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C++ program to replace all the lower-case letters of a given string with the corresponding capital letters.
Next: Write a C++ program to which reads n digits chosen from 0 to 9 and counts the number of combinations where the sum of the digits equals to given number. Do not use the same digits in a combination.
What is the difficulty level of this exercise?