I am beginning C++ programmer, I have been taking my c++ class for about 5 months now and this is the first time I have been completely stumped and I know for a fact it wont be the last.

My professor assigned an assignment for us to find the mean, median and mode of an array of numbers. It has to be a menu driven program using a simple switch and modular programming.

Now I have everything working but the mode. I have tried to figure it out on my own for almost 2 weeks now, I have asked my professor, he pointed me in the right direction but I could still not figure it out.

Here is the block I am having trouble with.

void mode()
{

	const int num = 10;
	int i[num];
	int p , MODE , count;
	
	for (int foo = 0; foo < num; foo++)
	{
		cout << " Enter the numbers you wish to compute " <<          (foo + 1) << " : ";
		cin >> i[foo];
	}
	
	for (int count = 0; count < num; count++)
	cout << i[count] << " ";
	cout << endl;
	
	sort(i , num);
	cout << " The values again sorted are : ";
	
	for (int count = 0; count < num; count++)
	cout << i[count] << " ";
	cout << endl;
	
	

		system("pause");
	
}

There is no function for mode in there yet however I do not know where to start. Any help would be greatly appreciated.

Thanks.
Tom

Recommended Answers

All 12 Replies

void mode()
{

	const int num = 10;
	int i[num];
	
	
	for (int foo = 0; foo < num; foo++)
	{
		cout << " Enter the numbers you wish to compute " << (foo + 1) << " : ";
		cin >> i[foo];
	}
	
	for (int count = 0; count < num; count++)
	cout << i[count] << " ";
	cout << endl;
	
	sort(i , num);
	cout << " The values again sorted are : ";
	
	for (int count = 0; count < num; count++)
	cout << i[count] << " ";
	cout << endl;
	
	
	int p , freq ,  MODE , count;
	
	for(count = 0; count < num; count++)
	{
		i[count] = 0;
	}
	for (count = 0; count < num; count++)
	{
		p = i[count];
		i[p]++;
	}
	for (count = 0; count < num; count++)
	{
		if (i[count] > p)
		{
			p = i[count];
			MODE = count;
		}
	}





	cout << "The mode is: " << MODE << endl;


		system("pause");
	
}

This is what I got im getting no errors but I am outputting 0 for my mode.

Why did you sort it?

http://en.wikipedia.org/wiki/Mode_(statistics)
To make it easier to count identical values, because they're next to each other?

I sorted it only because it was one of the requirements for the project. Would you like me to post the whole code insted of just the module?

1 1 2 3 3 3 3 3

Mode is 3

Run length of 1's is 2
Run length of 2's is 1
Run length os 3's is
See?

I know what mode is the most re occuring number. The problem for me is trying to translate it into c++ The way I was kind of told how to do it was to

Loop through the array
get the frequency of each number
use the highest frequency to find the mode
by storing the numbers of the highest frequency
then c out the mode

Now I may be totally wrong or just partially wrong. I just am trying to figure this out so I can understand this better.

If other "modules" (mean and median) look like this function mode I can't imagine how you "have everything working but the mode".
1. It seems you must "find the mean, median and mode of an array of numbers". If so, why you get array values in the function mode body? You need special module (or function) to get array values then find and print mean, median and mode of this array by separate functions. No needs to read array values again and again in every function.
2. About "modular programming". You print array twice (before and after sorting) with for loops. Is it a good example of "modularity"? Where is printarray function (module)? ;)
3. Read the mode term definition again. For example, see http://www.socialresearchmethods.net/kb/statdesc.php):

The mode is the most frequently occurring value in the set of scores

You have a sorted array and now all you need: count occurencies of every value and return a value corresponded to max counter. That's a code sceleton:

int currvalue = a[0]; // start from the 1st element
int counter = 1;
int maxcounter = 1;
int modevalue = a[0];
for (int i = 1; i < n; ++i) {
    if (a[i] == currvalue)
        ++counter;
    else { // next value started...
        if (counter > maxcounter) { // new mode candidate
            maxcounter = counter;
            modevalue = value;
        }
        currvalue = a[i]; // ready to count next values
        counter = 1;
    }
}
// Check last value, copy code...
if (counter > maxcounter) {
    maxcounter = counter;
    modevalue = value;
}

Strictly speaking, this snippet can report the only mode of unimodal arrays or the 1st mode of multimodal arrays. For example, it detects only mode 3 (and can't detect 4) for this array with two modes: { 1 3 3 2 4 4 5 }. You need the 2nd pass over array to detect all modes. The 1st pass calculate maxcounter, the 2nd one prints all values with counter == maxcounter.

Apropos, for the mode calculation only: you must sort an array to avoid additional memory allocation or O(N^2) compexity. In that case you need n*log(n) sorting routine. If you have used O(n^2) sorting, better use another O(n^2) approach: for every array element find its counter, get max counter then repeat and print elements with counter == maxcounter. No need to sort an array in that case.

However you need sorting to get median...

If other "modules" (mean and median) look like this function mode I can't imagine how you "have everything working but the mode".
1. It seems you must "find the mean, median and mode of an array of numbers". If so, why you get array values in the function mode body? You need special module (or function) to get array values then find and print mean, median and mode of this array by separate functions. No needs to read array values again and again in every function.
2. About "modular programming". You print array twice (before and after sorting) with for loops. Is it a good example of "modularity"? Where is printarray function (module)?
3. Read the mode term definition again. For example, see

Like I said before I am a NOOB in all means of the word ha.

but to answer your questions
1. I did that just because it made sense to me insted of confusing myself with function calls going everywhich way, We just learned function calls as well with arrays.

2. He wanted to see that we actually did sort the array so he asked us to print the un sorted numbers then the sorted numbers. Thats why that is there.
3. Im reading that right now. you say if the mean and median look anything like the mode, it doesn't the mode is the only screwed up looking to me. Im just trying to understand this a little bit better.

Is this number the same as the previous number?

- yes - do something
- no - do something else

what number? You lost me or I lost myself

If you mean the array numbers

I have just been using 1112345678

just to try and get this to work.

I see ArkM has already posted the actual answer, so trying to leave clues just isn't going to work.

@Salem no need to be mean ;) Thanks for helping me out I really apreciate it
@Arkm
Thank you for explaining it to me it was a little difficult for me to understand but I think I got it.
Thanks for all of your help

int getMode(int *movieWatched, int SIZE)
{
    int modenum, oldnum, curntnum = -1, precount, count = 0;
    for(int e = 0; e < SIZE; e++)
    {
        precount = 0;
        oldnum = *(movieWatched + e);
        for (int n = 0; n < SIZE; n++)
        {
            if(curntnum == oldnum)
            oldnum = *(movieWatched + (++e));

        }

        for(int i = 0; i < SIZE; i++)
            {

                if (oldnum == movieWatched[i])
                {
                    precount++;
                }
            }
        curntnum = oldnum;
        if (precount > count)
                        {
                            count = precount;
                            modenum = curntnum;
                        }
    }
    return (modenum);
}
commented: What does that have to do with anything?? -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.