Current Code:

void findMode(int *array, int size)
{
	int array2[size];
	int count;
	int x;
	int mode = 0;

	for (count = 0; count < size; count++)
	{
		x = array[count];
		array2[x] = array2[x]++;
	}

	for ( count = 0; count < size; count++)
	{
		if (array2[count] > mode)
		mode = array2[count];
	}
}

this is my curren jab at it. im trying to find what the mode is from the array. so far i created a 2nd array to hold the amount of times i see a number. the 2nd loop then finds the largest number (the amount of times ive seen a number).

at this point i have already set numbers to all the array and sorted them from smallest to largest. now im kinda lost on finding the mode.

something just hit me, If i find the largest value in the array before hand, i can just create an array large enough to hold 0 to that value. if that makes sense. this is what i tweeked it to:

void findMode(int *array, int size, int large)
{
	int array2[large];
	int count;
	int x;
	int mode = 0;

	for (count = 0; count < size; count++)
	{
		x = array[count];
		array2[x] = array2[x]++;
	}

	for ( count = 0; count < size; count++)
	{
		if (array2[count] > mode)
		mode = count;
	}
}

i am getting an error saying : error C2057: expected constant expression for "int array2[large];"
i know i want to make an array, but i guess i need to make it with a constant?
Am i on the right track here?

Yes, you must use a constant value in your array declaration. Recent change to C language allows use of a variable size, and "some" C++ compiler(s) now accept that usage, but it's not standard for C++ yet.

Using the values found as the index of the counting array is bound to cause problems, in any general use of such a function. Consider an array of size 100, where the values may range up to 1000. So, you would need to allocate very large array, and there may be much wasted space.

I can think to two relatively simple approaches to find mode.

a - use two arrays ( or a 2D array) in the function - store values found and a count of times found - you have to search through the array for each source value.

b - sort the data, then count the adjacent equal items.

Val

>> a - use two arrays ( or a 2D array) in the function - store values found and a count of times found - you have to search through the array for each source value.

i thought thats what i was doing... =/ i confussed now

>>b - sort the data, then count the adjacent equal items.

the code i have above this already sorts it from lowest to highest, which was my first intention, but i am unaware of how to keep track or similar numbers.

What I meant was to declare two arrays, or a 2D array, in the function. This is in addition to the array of data that is passed to it.

int counts[100] = { 0 }; //or whatever size your data array is. global constants help here1
int values[100] = { 0 };

Since your data is sorted, start at the beginning, grab the first item. Store that in element 0 of values, increment the counts array at that index.

Examine the next item in array, comparing to the item that came before it. If the same, increment the counts array at the index 0. If the item is different, store it in the next place in values, set counts at that index to 1. Repeat till you run out of data.

The you go through the counts array looking for the highest count, that index points to your mode.

Note: as I understand the definition of mode, if you have two or more values with the same occurrence count, you don't have a mode.

Val

Comments
Great Help

>> Note: as I understand the definition of mode, if you have two or more values with the same occurrence count, you don't have a mode.

thats not it at all. lol if anything its the other way around. A mode is the most occuring number.

example:
1 2 3 3 4 5 6 7

the mode is 3 since it occured twice and every other number occured once.

I found that i could actually make an array with the size that i wanted, now im currently in the process of trying to correctly find the mod. this is my current code:

void findMode(int *array, int size, int large)
{
	int *array2;
	int count;
	int x;
	int mode = 0;

	array2 = new int[large];

	for (count = 0; count < large; count++)
		array2[count] = 0;

	for (count = 0; count < size; count++)
	{
		x = array[count];
		array2[x] = array2[x]++;
	}

	for (count = 0; count < large; count++)
	{
		if (array2[count] > mode)
		mode = count;
	}
		cout << "The mode is: " << mode << endl;
}

Current output:

How many numbers do you wish in input?5
Enter the numbers you would like to find the mode of:
Intput number 1: 10
Intput number 2: 15
Intput number 3: 60
Intput number 4: 60
Intput number 5: 2
The mode is: 2

you already see the error. the mode is suppose to be 60.

WOOT finally got it to work. Thnks for all the help

Final Code:

void findMode(int *array, int size, int large)
{
	int *array2;
	int count;
	int x;
	int mode = 0;
	int highest = 0;

	array2 = new int[large];

	for (count = 0; count < large; count++)
		array2[count] = 0;

	for (count = 0; count < size; count++)
	{
		x = array[count];
		array2[x]++;
	}

	for (count = 0; count < large; count++)
	{
		if (array2[count] > highest)
		{
			highest = array2[count];
			mode = count;
		}
		
	}
		cout << "The mode is: " << mode << endl;
}

Test output:

How many numbers do you wish in input?5
Enter the numbers you would like to find the mode of:
Intput number 1: 4
Intput number 2: 6
Intput number 3: 6
Intput number 4: 8
Intput number 5: 2
The mode is: 6

array2 = new int[large]; Will hold counts for values 0 to large-1.

In your example, 60 is the value large. What happens when you access the array2 at index 60? There is no such place. When you are looping through finding the highest count, 60 is not found.

Create that array as: array2 = new int[large+1]; So, the loops that access that array must be written either as:

for (count = 0; count <= large; count++)
//or
      for (count = 0; count < large+1; count++)

In that loop, you are storing the location (the value of) the mode. You compare the count at each value to that location (index) - apples vs oranges. Here's how it should go:

for (count = 0; count <= large; count++)
	{
		if (array2[count] > array2[mode])
		mode = count;
	}
		cout << "The mode is: " << mode << endl;

Val

thanks,
ill take any help i can get.
i got about as far as you did i think you gave me an idea though
ill post it later.

This article has been dead for over six months. Start a new discussion instead.