Reputation: 3
I need a program to get 100 numbers between 0-20 and count the repition of the most repeated number.
Here is what I got for less amount for input (10 instead of 100) but ofc it's wrong.
#include <iostream>
using namespace std;
int main() {
int num, x,c;
for(int i=1; i<=10; i++) {
cin >> num;
if(num==x)
c++;
else
x=num;
}
cout << c;
return 0;
}
Upvotes: 0
Views: 146
Reputation: 29965
Assuming you mean the longest continuous sequence, here's one way you can do it:
#include <iostream>
int main() {
int max_val = 0, max_len = 0; // Overall longest
int cur_val = 0, cur_len = 0; // Current
for (int i = 0; i < 10; ++i) {
int val;
std::cin >> val; // read 1 number
if (val == cur_val) // if still counting the same, increment the length
++cur_len;
else { // else, set the max and reset current
if (cur_len > max_len) {
max_len = cur_len;
max_val = cur_val;
}
cur_len = 1;
cur_val = val;
}
}
// consider the very last sequence
if (cur_len > max_len) {
max_len = cur_len;
max_val = cur_val;
}
// Result
std::cout << "Longest seq: " << max_val << ", length: " << max_len << '\n';
}
Upvotes: 1