Statistical mode......

This is a discussion on Statistical mode...... within the C++ Programming forums, part of the General Programming Boards category; Is there a quick and dirty way for determining the mode (most occuring number) of inputted numbers. Only need mode ...

  1. #1
    Unregistered
    Guest

    Statistical mode......

    Is there a quick and dirty way for determining the mode (most occuring number) of inputted numbers. Only need mode for one number even though its possible to have multiple numbers with the same frequency.

    Thanks,
    Dave

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Not that I know of.

  3. #3
    Señor Member
    Join Date
    Jan 2002
    Posts
    561
    Hehe, I just had to do that. It was a big pain in the ass, but I got it to work. I'm not going to do your homework for you (not just because it is homework, but I am lazy), but I will give you a hint. Make an array of numbers from 1 to a hundred, lets call it array. array[5] would equal how many times 5 occured. That should be enough info to get it to work.

  4. #4
    Unregistered
    Guest

    Thanks

    I was wrestling around with an approach and you gave me one.

    Thanks,

    Dave

  5. #5
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    That's a silly idea. I thought of it, but what if you are using integers from 0-65535? A 128k array? Not a good idea. Here's my idea.

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    void sortNums (int *n);
    int getFreq (int *n, int *mostCommon, int *frequency);	// Returns size of run-length found, regardless of whether it's the biggest.
    
    void main (void)
    {
    	int i;
    
    	int num [10];
    
    	int mostCommon;
    	int frequency = 0;
    
    	for (i = 0; i < 10; i++)
    	{
    		cout << "Num: ";
    		cin >> num [i];
    	}
    
    	sortNums (num);
    
    	for (i = 0; i < 10; i++)
    		i += (getFreq (num + i, &mostCommon, &frequency) - 1);
    
    	cout << "Most common: " << mostCommon << endl;
    }
    
    void sortNums (int *n)
    {
    	int i, j;
    	int temp;
    
    	for (i = 0; i < 9; i++)
    		for (j = i; j < 10; j++)
    		{
    			if (n[j] > n[i])
    			{
    				temp = n[i];
    				n[i] = n[j];
    				n[j] = temp;
    			}
    		}
    }
    
    int getFreq (int *n, int *mostCommon, int *frequency)
    {
    	int f = 1;
    	int startNum = *n;
    
    	n++;    // Pointer arithmetic.
    	while (*n == startNum)	// Find run-lengths.
    	{
    		 f++;    	// We've found one more.
    		 n++;			// Onto next element.
    	}
    
    	if (f > *frequency)	// If more of this number than current most common:
    	{
    		*frequency = f;
    		*mostCommon = startNum;
    	}
    
    	return f;
    }
    That's tested and it works!!!
    MSVC++ 6.0

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    11
    Originally posted by samGwilliam
    That's a silly idea. I thought of it, but what if you are using integers from 0-65535? A 128k array? Not a good idea. Here's my idea.

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    void sortNums (int *n);
    int getFreq (int *n, int *mostCommon, int *frequency);	// Returns size of run-length found, regardless of whether it's the biggest.
    
    void main (void)
    {
    	int i;
    
    	int num [10];
    
    	int mostCommon;
    	int frequency = 0;
    
    	for (i = 0; i < 10; i++)
    	{
    		cout << "Num: ";
    		cin >> num [i];
    	}
    
    	sortNums (num);
    
    	for (i = 0; i < 10; i++)
    		i += (getFreq (num + i, &mostCommon, &frequency) - 1);
    
    	cout << "Most common: " << mostCommon << endl;
    }
    
    void sortNums (int *n)
    {
    	int i, j;
    	int temp;
    
    	for (i = 0; i < 9; i++)
    		for (j = i; j < 10; j++)
    		{
    			if (n[j] > n[i])
    			{
    				temp = n[i];
    				n[i] = n[j];
    				n[j] = temp;
    			}
    		}
    }
    
    int getFreq (int *n, int *mostCommon, int *frequency)
    {
    	int f = 1;
    	int startNum = *n;
    
    	n++;    // Pointer arithmetic.
    	while (*n == startNum)	// Find run-lengths.
    	{
    		 f++;    	// We've found one more.
    		 n++;			// Onto next element.
    	}
    
    	if (f > *frequency)	// If more of this number than current most common:
    	{
    		*frequency = f;
    		*mostCommon = startNum;
    	}
    
    	return f;
    }
    That's tested and it works!!!
    But what if there's more than one mode?

  7. #7
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Well, there's not a lot you can do about that is there? The guy said he only wanted one. The program will give the highest out of duplicate modes. To get the lowest, flip the > operator in the bubble-sort function.

    I digress. Here are some optimisations:

    Code:
    for (i = 0; i < 10;)
       i += getFreq (num + i, &mostCommon, &frequency);
    And you could also declare frequency as a static variable inside getFreq instead.
    MSVC++ 6.0

  8. #8
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Bloody smileys!!!
    MSVC++ 6.0

  9. #9
    Unregistered
    Guest

    Thanks

    Thanks,

    Like you said his approach had no flexablity to it.

    I figured for something like this someone already has a solution and no need to always reinventing the wheel.

    I hear some companies keep routine things like this on file to save time when it comes to programming and not always reinvent the wheel when it has been invented.

    thanks,
    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Statistical Mode Function
    By h4rrison.james in forum C Programming
    Replies: 5
    Last Post: 01-16-2009, 08:48 PM
  2. console mode and service mode
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 06-01-2008, 01:42 AM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 10:06 AM
  4. Implementing "ls -al"
    By pdstatha in forum Linux Programming
    Replies: 11
    Last Post: 03-20-2002, 03:39 AM
  5. free() usage
    By pdstatha in forum C Programming
    Replies: 5
    Last Post: 03-13-2002, 08:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21