Finding The Most Common Value and its Count in a Array of Random Numbers

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.

My Code:
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace 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.
Last edited on
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

int main()
{
    std::srand( std::time(nullptr) ) ;

    const int N = 50 ;
    int array[N] ;

    const int UBOUND = 100 ; // non-negative numbers < UBOUND

    for( int i = 0 ; i < N ; ++i ) array[i] = std::rand() % UBOUND ;

    const int 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#for
    for( 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" ;
}

http://coliru.stacked-crooked.com/a/a46b4e0d06c6d0d4
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
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

//======================================================================

void printArray( const vector<int> &A, int rowLength, int width = 2 )
{
   int col = 0;
   for ( int e : A )
   {
      cout << setw( width ) << e << ' ';
      if ( ++col == rowLength ) 
      {
         cout << '\n';
         col = 0;
      }
   }
}

//======================================================================

template<typename T> vector<T> mode( const vector<T> &values, int &maxFreq )
{
   map<T,int> freq;
   for ( auto e : values ) freq[e]++;

   maxFreq = 0;
   for ( auto p : freq ) if ( p.second > maxFreq ) maxFreq = p.second;

   vector<T> result;
   for ( auto p : freq ) if ( p.second == maxFreq ) result.push_back( p.first );
   
   return result;
}

//======================================================================

int main()
{
   srand( time( 0 ) );

   const int NUMVALUES = 50, LIMIT = 100, ROWLENGTH = 10;
   vector<int> A( NUMVALUES );
   generate_n( A.begin(), NUMVALUES, [ LIMIT ](){ return rand() % LIMIT; } );
   printArray( A, ROWLENGTH );
   
   int maxFreq;
   cout << "\nMost common is/are: ";
   for ( auto e : mode( A, maxFreq ) ) cout << e << ' ';
   cout << "with frequency " << maxFreq << '\n';
}


74 61  1 53 34 28 31 52 86 73 
93 75 97 55 82 39 88 94 16 41 
47 54 75 23 50 42 66 13 33 31 
46 24 43 30 31 80 93 16 64 52 
76 77 29 59  4 88 66 16 76 28 

Most common is/are: 16 31 with frequency 3
Last edited on
Topic archived. No new replies allowed.