How to show the mode using array

How do I incorporate finding the mode? I know for this there wouldn't be mode as the numbers are 10, 20, 30, 40, 50. But if I did have a duplicate of a number how would I go about doing it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
        #include "pch.h"
        #include <iostream>
        using namespace std;

        int main() {
        #define MAX 5 

	int a;

	int myArray[MAX];

	for (a = 0; a < MAX; a++) { myArray[a] = (a + 1) * 10; }

	for (a = 0; a < MAX; a++) { cout << myArray[a] << " "; }

	cout << endl;  
	cout << "The Median is "; cout << myArray[5 / 2] << " "; // median 
        for odd numbers
	
	system("PAUSE");   
	return 0;
}
Hello Rocketboy,

The first thing I see is #define MAX 5 . Although this may work it is most often written as constexpr size_t MAXSIZE{ 5 };. In simple terms "size_t" is another name for "unsigned int". You should get use to using "size_t" because there a many member functions that you will be using that return a "size_t".

Next is system("pause");. I most often see this with lower case letters, but it may not make any difference. In the end this is best not to use as it could be a problem.

When writing a program I use this in place of the "system("pause");":
1
2
3
4
5
6
// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
// <--- Used to keep the console window open in Visual Studio Debug mode.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();


As you said with the numbers that you put in your array there is not mode.

What you can do is
1
2
3
4
5
costexpr size_t MAXSIZE{ 10 };

int myArray[MAXSIZE]{ 10, 20, 30, 10, 40, 50, 10, 60, 70, 10 };

//for (a = 0; a < MAXSIZE; a++) { myArray[a] = (a + 1) * 10; } 


This is a good trick for testing. When you define and initialize the array with given numbers you not only eliminate the need for user input and can skip that part, but you are using known values that will produce a known result.

From what I have gathered it looks like you will have to check the array to find out which number is repeated and I believe how often. Also the numbers have to be in ascending order.

Hope that helps,

Andy
No mode or multimodal?
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
#include <iostream>
#include <chrono>
#include <random>
#include <vector>
#include <map>
#include <algorithm>

int main ()
{
  using std::cout;
  unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();
  std::mt19937 g1( seed1 ); // inadequate, but will do for now
  std::uniform_int_distribution<int> distribution( 5, 10 );

  std::vector<int> foo( 20 );
  for ( auto & x : foo ) {
    x = distribution( g1 );
    cout << ' ' << x;
  }
  cout << '\n';

  std::map<int,size_t> freq;
  for ( auto x : foo )
    freq[x] = std::count( foo.begin(), foo.end(), x );

  auto max = *std::max_element( freq.begin(), freq.end(),
                    [](auto l, auto r){return l.second < r.second;} );

  for ( auto y : freq ) {
      cout << y.first << ':' << y.second;
      if ( y.second == max.second ) cout << '*';
      cout << '\n';
  }
}
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
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;

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

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

   int maxFreq = ( *max_element( freq.begin(), freq.end(), []( auto a, auto b ){ return a.second < b.second; } ) ).second;
   mode.clear();
   for ( auto p : freq ) if ( p.second == maxFreq ) mode.push_back( p.first );
   
   return maxFreq;
}

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

int main()
{
   vector<int> days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

   vector<int> imode;
   cout << "Modal count is " << modeCount( days, imode ) << " for month(s) with number of days ";
   for ( int i : imode ) cout << i << " ";
   cout << '\n';

   //---------------

   string sentence;
   cout << "\nEnter a sentence: ";   getline( cin, sentence );
   vector<char> letters;
   for ( char c : sentence ) if ( isalpha( c ) ) letters.push_back( tolower( c ) );

   vector<char> cmode;
   cout << "Modal letter count is " << modeCount( letters, cmode ) << " for letter(s) ";
   for ( char c : cmode ) cout << c << " ";
   cout << '\n';
}


Modal count is 7 for month(s) with number of days 31 

Enter a sentence: Happy days!
Modal letter count is 2 for letter(s) a p y 
One more slight variant:
1
2
3
4
5
6
7
8
9
10
11
12
template<typename T> int modeCount( const vector<T> &values, vector<T> &mode )
{
   int maxFreq {};
   map<T,int> freq;
   for ( auto e : values ) {
       if ( maxFreq < ++freq[e] ) maxFreq = freq[e];
   }
   mode.clear();
   for ( auto p : freq ) if ( p.second == maxFreq ) mode.push_back( p.first );
   
   return maxFreq;
}



@Rocketboy:
Do not get scared about all the characters that we post.
Can you see the core of the issue within the blurbs of code?
1
2
3
   for ( auto e : values ) {
       if ( maxFreq < ++freq[e] ) maxFreq = freq[e];
   }


Ah, neat!!
I'm completely confused by all of that
I'm completely confused by all of that

No wonder, it's quite advanced stuff they were throwing at you.

Are you sure that you need to find the mode of the array?
I ask because you print the median of the array on line 17.
Topic archived. No new replies allowed.