Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding the mode

Status
Not open for further replies.

gonzilla

Programmer
Apr 10, 2001
125
US
Hi,

I'm trying to write a fuction which takes a two dimensional array of integer scores and finds the mode. Right now, the array is hard coded but I want it to be able to change if the number of scores changes. I'm a little stumped. There is probably something in math.h that does this, but I have to write it from scratch.

My idea so far is to read the 2-d array and store the count of each different possible score in another integer array and pass back the array index. Is there a better way of doing this w/out having to use recursion? And, what happens if two different scores have the same count and it's the highest?

Thanks for your help.

-Tyler
 
I can't understand, you have problems with memory allocation? If so, use realloc. If not, try to be more detail. Ion Filipski
1c.bmp


filipski@excite.com
 
If I remember correctly... the mode is the number that is in the middle? and if an even number of values exist it is the number above and below the middle averaged?

i.e.

1,2,3,4,5,6 mode = (3+4)/2
1,2,3,4,5 mode = 3

am I correct in saying this?

If so... sort your numbers from lowest to highest and just take the center number or average in the case of an even count.

Matt
 
Hi,

Actually the definition of mode is - The number or range of numbers in a set that occurs the most frequently. For example, if the numbers are:

10, 65, ,75, 100, 88, 65 , 75, 23, 65

The 65 appears 3 times, so it is the mode. I guess if 75 appeared 3 times as well, the mode would then be 65 and 75, but I'll check on that.

I don't think memory allocation is any problem at all. Sorry if the first submission was unclear.

Thanks.

-Tyler
 
Ahhh... i was either doing median or mean... i was never good at statistics. Anyhoo... what I would do then is make an array of the range of grades

int array[100];

and then do a memset on it

memset(array,0,sizeof(array));

then go throught the 2d array and use the value of the 2d array as an index into the array above

array[twod_array[j]]++;


Whoever has the largest number in the end wins. This is the clearest way to explain this. If however you are reading the numbers in one at a time then you could just keep a running total or use a struct with a counter in it.

Just some ideas

Matt
 
Thanks for your help.

I used the 2d_array[][] as the index and it worked great!

My only question is this...how do I keep track of the fact that there might be more than 1 mode? I mean, what if 60 appeared 5 times but so does 90? Or any number of them could appear equally the most.

I think my idea for switching which index becomes the mode could be modified to account for this problem, but I need some help as I am stumped at the moment. Here is my code for the mode function. I pass it a pointer to the 2d array and MODE and return the index. MODE is passed so I can modify it and print it in main after the return.

int mode (int const (*grad_ar)[7], int * MODE)
{
int temp_grade[101] = {0};
int i, j, r, index;
i = j = r = 0;

for (i = 0; i < STUDENTS; i++)
for (j = 0; j < 7; j++)
temp_grade[grad_ar[j]]++;

while (r++ < ((sizeof (temp_grade)) / (sizeof (int))) - 1)
{
if (temp_grade[r] != 0)
{
if (temp_grade[r] > cnt)
{
index = temp_grade[r];
*MODE = r;
}
}
}
return index;
}

Thanks for your help again.

-Tyler
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top