i know there is probably a trick for this in C that i do not know, but i attempted to write my own function for finding the mode of an array of data (As large as 1500 doubles).

Heres my crack at it.

void mode (double * array, int numItems)
{
	double array_saved_noMulti[numItems];
	int array_count[numItems], i = 0, n = 0, count = 0, max_count = 0, index = 0, index_2 = 0;
	
	for (i = 0, n = 0; i < numItems; i++) 
	{
		if (array[i] != array[i+1]) 
		{
			array_saved_noMulti[n] = array[i];
			n++;
		}
	}
	
	for (i = 0, n = 0; n < numItems; n++)
	{ 
		while (array[i] == array[i + 1])
		{
			count++;
			i++;
		}
		array_count[n] = count++;
		n++;
		count = 0;
	}
	
	array_count[0] = max_count;
	
	for (i = 0; i < numItems; i ++)
	{
		if (array_count[i] > max_count) 
		{
			array_count[i] = max_count;
			index = i;
		}
		if (array_count[i] = max_count) 
		{
			index_2 = i;
		}
	}
	
	printf("The mode is %lf or %lf, which occurs %i times", array_saved_noMulti[index], array_saved_noMulti[index_2], max_count);
}

if there is an easier way to do this please enlighten, it would make my day.

You need an outer "overall" loop (usually a for loop, let's say)

for(each number) {
   j=1;
   while(array[i] == array[i+j] { //because they're *sorted*
      ++j;
   }
   if(j > maxRepeats)
     mode = array[i];
}

That's to get you started, you may have one off errors in it -- it's very late here.

The key thing is, since the numbers are sorted, the repeating numbers, will be right next to each other. So a while or do while loop, is an intuitive way to do it.

wonderful. I could easily see where you were going with this so it was easy to figure out the rest. Thanks so much.

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.