Thread: Calculating Mode

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    25

    Calculating Mode

    Hello again. I'm looking to calculate a mode in an array, but I've run into a few problems with the syntax. I believe my general methodology would work well, though:

    1. Go through the array
    2. compare current index with the next index
    3. up a counter each time they are equal
    4. store each count value into a second array of pointers
    5. find the largest value in the second array
    6. print this value
    7. print the value that the second array value points to

    By all means, correct anything that's incorrect, improper or otherwise inefficient in the above. Here is my code snippet so far:

    Code:
    void hurrmode (int array[], int size)
    {
    	int i, j, *count, *array_two;
    
    	for (i = 0; i < size; i++)
    	{
    		for (j = i + 1; j < size; j++)
    		{
    			if (array[i] == array[j])
    			{
    				count ++;
    			}
    		}
    	}
    }
    I can't seem to figure out how to store the count values into a second array without getting a syntax error. I'm still not super familiar with the language, unfortunately.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't really need an array of pointers, just an array of numbers. *foo makes a pointer named foo, but it doesn't actually make it point to any memory. You could instead just do:
    Code:
    int foo[ HOWMANY ];
    Initialize them all to 0 first, then increment whatever you need. I'm not really sure that's what you would want to be doing here, but that's how you would do what it is you think you should be doing.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    25
    Thanks for the quick response. Hmmmm. I'm afraid I don't quite understand what you mean. I went ahead and changed some things around to make it simpler, though.

    Code:
    void hurrmode (int array[], int size)
    {
    	int i, j, k, count = 0, array_two[5], biggest = 0;
    
    	for (i = 0; i < size; i++)
    	{
    		for (j = i + 1; j < size; j++)
    		{
    			if (array[i] == array[j])
    			{
    				count ++;
    			}
    			array_two[j] = count;
    		}
    	}
    
    	for (k = 0; k < size; k++)
    	{
    		if (array_two[k] > biggest)
    		{
    			biggest = array_two[k];
    		}
    	}
    
    	printf("Mode occurs %i times", biggest);
    }
    Okay, so this works (yes yes yes)! However, there are two things I would like to do with what I have right now. For starters, I want array_two to be of of size size, not 5. Secondly, I'd also like to print out the value of the mode in addition to just how often it occurs.

    Would the second part require pointers, or would what you're saying work (I don't fully understand what you're saying, though)?

    edit: corrected some mistakes, spelling, etc
    Last edited by Amberlampz; 08-03-2011 at 09:47 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Amberlampz View Post
    For starters, I want array_two to be of of size size, not 5.
    C99 will let you do that:
    Code:
    void foo( int size )
    {
        int array[ size ];
    }
    Quote Originally Posted by Amberlampz View Post
    Secondly, I'd also like to print out the value of the mode in addition to just how often it occurs.
    Lets say we have an array and we want to print its contents, along with where they are in the array (because I don't know what you are asking exactly):
    Code:
    for( x = 0; x < size; x++ )
        printf( "array[ %d ] has %d for a value\n", x, array[ x ] );
    Is that what you want to do?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    25
    Quote Originally Posted by quzah View Post
    C99 will let you do that:
    Code:
    void foo( int size )
    {
        int array[ size ];
    }
    Unfortunately the compiler I'm using is Visual Studio 2010 (and they apparently have no plans to implement it). Is there a workaround, or would you suggest another compiler?

    Quote Originally Posted by quzah View Post
    Lets say we have an array and we want to print its contents, along with where they are in the array (because I don't know what you are asking exactly):
    Code:
    for( x = 0; x < size; x++ )
        printf( "array[ %d ] has %d for a value\n", x, array[ x ] );
    Is that what you want to do?


    Quzah.
    Not quite. I'm looking to print out the value of the mode of an array and how many times that value occurs. I have the latter, but I'm not sure (syntactically) how to print out the value itself. Apologies if I wasn't clear about that.

    I'm thinking that's going to require pointers, but I'm not sure.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Amberlampz
    Is there a workaround
    The "workaround" is actually the more common method: use dynamic memory allocation with say, malloc and free from <stdlib.h>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Amberlampz View Post
    Not quite. I'm looking to print out the value of the mode of an array and how many times that value occurs. I have the latter, but I'm not sure (syntactically) how to print out the value itself. Apologies if I wasn't clear about that.
    I'm still not clear on what you can't figure out how to print.
    Code:
    int x = putsomevaluehere;
    printf( "x is %d\n", x ); /* print whatever value x has */
    So, assuming you actually have a value stored some place, or can figure out how to store it some place, you should be able to print that value out. So:
    Code:
    int data[] = { 1, 2, 3, 1, 1, 2, 5 }; /* <--- has v */
    int mode[] = { 0, 3, 2, 1, 0, 1 }; /* 0 0s, 3 1s, 2 2s, etc... */
    int x;
    for( x = 0; x < 6; x++ ) /* because we happen to have six slots in our array */
        printf( "mode[ %d ] has the value of %d\n", x, mode[ x ] );
    As I illustrated earlier.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Not quite. I'm looking to print out the value of the mode of an array and how many times that value occurs. I have the latter, but I'm not sure (syntactically) how to print out the value itself. Apologies if I wasn't clear about that.
    Make it so the tally in array_two[2] means the values of 2 in array and so forth.

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    25
    Quote Originally Posted by laserlight View Post
    The "workaround" is actually the more common method: use dynamic memory allocation with say, malloc and free from <stdlib.h>.
    Noted, and thanks.

    Quote Originally Posted by quzah View Post
    I'm still not clear on what you can't figure out how to print.
    Given any set of numbers stored in an array, I compare an array index with the one above it, and if they're equal I increment a counter. These counter values are then stored into a second array. What I would like is to print out the value of the number that corresponds to the highest counter.

    The actual syntax of printing out the value (like how you gave an example) isn't the issue, but rather creating a variable that corresponds to the value is what's troubling me. My bad for not being super clear on that.

    Quote Originally Posted by whiteflags View Post
    Make it so the tally in array_two[2] means the values of 2 in array and so forth.
    So make the tally a pointer referencing the array index?

    I think I'm going to have to read up on pointers more if I go any further with this, provided it's the case that I do need to use pointers. Not familiar with them at all.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Amberlampz View Post
    Hello again. I'm looking to calculate a mode in an array, but I've run into a few problems with the syntax. I believe my general methodology would work well, though:

    1. Go through the array
    2. compare current index with the next index
    3. up a counter each time they are equal
    4. store each count value into a second array of pointers why?
    5. find the largest value in the second arraywhy?
    6. print this value
    7. print the value that the second array value points toYou don't need a second array
    Ok, it seems we have gotten off track here. To go back to your original question - How to find the mode of an array. Correct? The mode, is simply the value in the array that occurs the most. That is it, there is no need for two arrays or pointers or anything like that. This really is a simple max problem. Something like:
    Code:
    #include <stdio.h>
    
    #define ARRAYSIZE 10
    
    int main(void){
    
    	int myArray[ARRAYSIZE] = {8,3,4,5,2,3,2,2,7,2};
    	int count=1, max=0, index=0;
    
    	for(int i = 0; i < ARRAYSIZE;i++){
    		for(int j = i+1; j < ARRAYSIZE;j++){
    			if(myArray[i]==myArray[j])
    				count++;
    		}
    		if(count > max){
    			max = count;
    			index = i;
    		}
    		count = 1;
    	}
    
    	printf("The mode of myArray is %d, and occurs %d times\n",myArray[index],max);
    	getchar();
    	return(0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Amberlampz View Post
    Given any set of numbers stored in an array, I compare an array index with the one above it, and if they're equal I increment a counter.
    I'm not sure how this is giving you a mode. You are comparing array[ 1 ] with array[ 0 ] to see if they are the same? So basically all you are doing is looking for two consecutive numbers in an array? That's not mode as far as I know.
    Quote Originally Posted by Amberlampz View Post
    These counter values are then stored into a second array. What I would like is to print out the value of the number that corresponds to the highest counter.
    I already showed you that.

    Array 1 holds values of a bunch of data.
    Array 2 holds values of indexes related to the first array.
    Code:
    index = array2[ somespot ];
    printf( "array1[ index ] holds value of %d\n", index, array1[ index ] );

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    If the OP just wants to calculate the mode, e.g. "one of a range of values that has the highest frequency as determined statistically", there is no need for a second array. In fact this whole thing boils down to a simple max program as I pointed out above. However, I agree with you in that if the OP wants something else they need to be a lot more specific as to what they require.

    I think the basic problem here is that the OP has no clue what they want, something of a norm around here unfortunately.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Registered User
    Join Date
    Jul 2011
    Posts
    25
    Quote Originally Posted by AndrewHunter View Post
    Ok, it seems we have gotten off track here. To go back to your original question - How to find the mode of an array. Correct? The mode, is simply the value in the array that occurs the most. That is it, there is no need for two arrays or pointers or anything like that. This really is a simple max problem. Something like:

    <AndrewHunter's code>
    Wow. It's almost hilarious how the simpler answers are the hardest to come by. After sleeping on it I deduced that pointers wouldn't really be needed unless I wanted to over-complicate things. Anyways, thanks. Code works! If it's any consolation, I've drawn out a flowchart on some paper to understand the process in addition to shamelessly jacking and adjusting your code.

    Quote Originally Posted by quzah View Post
    I'm not sure how this is giving you a mode. You are comparing array[ 1 ] with array[ 0 ] to see if they are the same? So basically all you are doing is looking for two consecutive numbers in an array? That's not mode as far as I know.I already showed you that.

    Array 1 holds values of a bunch of data.
    Array 2 holds values of indexes related to the first array.
    Code:
    index = array2[ somespot ];
    printf( "array1[ index ] holds value of %d\n", index, array1[ index ] );

    Quzah.
    I guess I'm just not explaining it well enough. Forgive me, but my experience with C is pathetically small (one month and change) so I'm not completely up to speed with everything yet. I imagine it's like trying to glean information from a toddler (me) for you, so thanks for being patient regardless.

    Quote Originally Posted by AndrewHunter View Post
    If the OP just wants to calculate the mode, e.g. "one of a range of values that has the highest frequency as determined statistically", there is no need for a second array. In fact this whole thing boils down to a simple max program as I pointed out above. However, I agree with you in that if the OP wants something else they need to be a lot more specific as to what they require.

    I think the basic problem here is that the OP has no clue what they want, something of a norm around here unfortunately.
    Nope, nothing too specific. Just wanting to familiarize myself with the language. Unfortunately there seems to be a slight communication barrier between myself, who is very inexperienced, and you guys, who have obviously been doing this for a long time. You could chalk it up to inexperience, but hopefully that won't be the case in a couple of months after coding some more!

    Thanks for the patience, especially Quzah. I know how frustrating it is to deal with somebody that can't even communicate information properly, so big kudos and I'll be thanking your posts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-24-2010, 01:35 PM
  2. Replies: 4
    Last Post: 09-16-2006, 07:11 PM
  3. calculating the mode
    By bigggame in forum C Programming
    Replies: 10
    Last Post: 06-13-2006, 03:04 AM
  4. Calculating mode
    By Lord CyKill in forum C Programming
    Replies: 1
    Last Post: 09-25-2003, 02:41 AM
  5. inverse mode and flash mode for text
    By Jaguar in forum Linux Programming
    Replies: 2
    Last Post: 02-08-2003, 12:47 PM