Hello, Can someone please help me with the C++ code for finding 2frequent number
ID: 3618124 • Letter: H
Question
Hello,Can someone please help me with the C++ code for finding 2frequent numbers in an array?
here's what I have so far: I found this on a website, but it'sonly for finding the mode for 1 number.
Thanks!
#include <iostream> using namespace std;
const int n = 10;
int main() {
int a[n] = {1,2,2,2,2,3,3,3,3,5}; int x = 1;
int arr[n]; int j=0;
int currvalue = a[0]; // start from the 1st element int counter = 0; int maxcounter = 1; int modevalue = a[0]; for (int i = 1; i < n; ++i) { if (a[i] == currvalue) { ++counter; cout << "Counter: " << counter <<endl; cout << " Number: " << a[i] << endl; } else { // next value started... if (counter >maxcounter) { // new mode candidate maxcounter = counter; modevalue =currvalue; if(counter == maxcounter) {
arr[j] = currvalue; j++;
cout << "Number: " << arr[j] << endl; } }
currvalue = a[i]; // ready tocount next values counter = 1; }
} // Check last value, copy code...
for (j = 0; j < n; j++) { cout << "N:" << arr[j] << endl; }
return 0; }
Can someone please help me with the C++ code for finding 2frequent numbers in an array?
here's what I have so far: I found this on a website, but it'sonly for finding the mode for 1 number.
Thanks!
#include <iostream> using namespace std;
const int n = 10;
int main() {
int a[n] = {1,2,2,2,2,3,3,3,3,5}; int x = 1;
int arr[n]; int j=0;
int currvalue = a[0]; // start from the 1st element int counter = 0; int maxcounter = 1; int modevalue = a[0]; for (int i = 1; i < n; ++i) { if (a[i] == currvalue) { ++counter; cout << "Counter: " << counter <<endl; cout << " Number: " << a[i] << endl; } else { // next value started... if (counter >maxcounter) { // new mode candidate maxcounter = counter; modevalue =currvalue; if(counter == maxcounter) {
arr[j] = currvalue; j++;
cout << "Number: " << arr[j] << endl; } }
currvalue = a[i]; // ready tocount next values counter = 1; }
} // Check last value, copy code...
for (j = 0; j < n; j++) { cout << "N:" << arr[j] << endl; }
return 0; }
#include <iostream> using namespace std;
const int n = 10;
int main() {
int a[n] = {1,2,2,2,2,3,3,3,3,5}; int x = 1;
int arr[n]; int j=0;
int currvalue = a[0]; // start from the 1st element int counter = 0; int maxcounter = 1; int modevalue = a[0]; for (int i = 1; i < n; ++i) { if (a[i] == currvalue) { ++counter; cout << "Counter: " << counter <<endl; cout << " Number: " << a[i] << endl; } else { // next value started... if (counter >maxcounter) { // new mode candidate maxcounter = counter; modevalue =currvalue; if(counter == maxcounter) {
arr[j] = currvalue; j++;
cout << "Number: " << arr[j] << endl; } }
currvalue = a[i]; // ready tocount next values counter = 1; }
} // Check last value, copy code...
for (j = 0; j < n; j++) { cout << "N:" << arr[j] << endl; }
return 0; }