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
|
#include <iostream>
using namespace std;
int find_count(int a[], int size, int index);
int most_frequent(int a[], int size, int& frequency);
int main()
{
int arr[] = { 2, 7, 5, 6, 7, 1, 6, 2, 1, 7 };
int firstElement = find_count(arr, 10, arr[0]);
int secondElement = find_count(arr, 10, arr[1]);
cout << "The first element in array occurs " << firstElement << " times\n";
cout << "The second element in array occurs " << secondElement << " times\n";
int freq = 0;
int best = arr[most_frequent(arr, 10, freq)];
cout << "The most frequent number in the array is: "
<< best << " (" << freq << " x)." << endl;
system("pause");
return 0;
}
int find_count(int a[], int size, int index)
{
int counter = 0;
for (int i = 0; i < size; i++)
{
if (index == a[i])
counter++;
}
return counter;
}
int most_frequent(int a[], int size, int& frequency)
{
int max = a[0], index_of_max = 0;
for (int i = 1; i < size; i++)
{
if (a[i] >= max)
{
max = a[i];
index_of_max = i;
frequency++;
}
}
return index_of_max;
}
|