This program I'm working on generates an array of random numbers, then outputs the most common number in the array and how many times it appears. I already managed to output the array of random numbers, but I'm having trouble with outputting the most common number in the array along with its count. I provided my current code and output below along with the expected output. Any hints given on how to output a common value and its count from an array of random numbers will help out a lot.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int array[50];
int r;
int common = array[0];
int max_count = 0;
int count = 1;
int main()
{
cout << "Array of 50 Random Numbers:";
cout << endl;
srand (time(0));
for (int i = 0; i < 50; i++)
{
r = rand() % 100;
cout << r << " ";
if (i % 10 == 9)
cout << endl;
}
for (int i = 0; i < 50; i++)
{
r = rand() % 100;
for (r = i + 1; r < 50; r++)
{
if (array[i] == array[r])
count++;
if (count > max_count)
max_count = count;
}
}
for (int i = 0; i < 50; i++)
{
r = rand() % 100;
for (int r = i + 1; r < 50; r++)
{
if (array[i] == array[r])
count++;
if (count == max_count)
cout << array[i] << endl;
}
}
for (int i = 0; i < 50; i++)
{
for (int r = i + 1; r < 50; r++)
{
if (array[r] == array[i])
{
max_count++;
if (max_count > count)
{
common = array[i];
count = max_count;
}
}
}
}
cout << endl;
cout << "The most common number is "
<< common << ", which appears " << count
<< " times." << endl;
return 0;
}
My Output:
1 2 3 4 5 6 7 8
Array of 50 Random Numbers:
50 64 41 18 4 55 5 99 29 81
92 51 0 54 84 21 3 0 30 27
17 1 56 31 54 79 73 92 27 69
74 40 89 35 99 1 26 85 56 1
33 88 37 18 23 1 99 54 56 2
The most common number is 0, which appears 2451 times.
Expected output:
1 2 3 4 5 6 7 8
Array of 50 Random Numbers:
50 64 41 18 4 55 5 99 29 81
92 51 0 54 84 21 3 0 30 27
17 1 56 31 54 79 73 92 27 69
74 40 89 35 99 1 26 85 56 1
33 88 37 18 23 1 99 54 56 2
The most common number is 1, which appears 4 times.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
int main()
{
std::srand( std::time(nullptr) ) ;
constint N = 50 ;
int array[N] ;
constint UBOUND = 100 ; // non-negative numbers < UBOUND
for( int i = 0 ; i < N ; ++i ) array[i] = std::rand() % UBOUND ;
constint LINE_SZ = 10 ;
for( int i = 0 ; i < N ; ++i )
{
std::cout << std::setw(3) << array[i] ;
if( i%LINE_SZ == (LINE_SZ-1) ) std::cout << '\n' ;
}
// array to keep counts of how many times a number appears
// at the end, number n has appeared count[n] times
int count[UBOUND] = {0} ; // initialise to all zeroes
int max_count = 0 ; // maximum count seen so far
int most_common_number = -1 ; // most common number so far
// range based loop: http://www.stroustrup.com/C++11FAQ.html#forfor( int number : array ) // for each number in the array
{
++count[number] ; // update its count
if( count[number] > max_count )
{
max_count = count[number] ;
most_common_number = number ;
}
}
// note: this prints out one of the most common numbers.
// ie. if all three of 43 , 55 and 67 appear most often, say four times each,
// this would print out only one of them
std::cout << "\nthe most common number is " << most_common_number
<< ", it appears " << max_count << " times.\n" ;
}