Finding the mode of an array

This is a discussion on Finding the mode of an array within the C++ Programming forums, part of the General Programming Boards category; How would i go about finding the mode (the number that appears most frequently) in a list of numbers i ...

  1. #1
    Shnakepup
    Guest

    Finding the mode of an array

    How would i go about finding the mode (the number that appears most frequently) in a list of numbers i put in an array? I'm in a comp. science class right now, and i just can't think of how i would do it.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    One way would be to loop through the numbers in the array and put them into a map<int,int> container where the key stores the number from the input array and the value represents the number of times that value has occured so far. Each time you add a key to the map, you increment its associated value. What results is essentially a sorted array (sorted by key) that stores the frequency, i.e. number of occurances, that a particular value has occured in the input array. You could then search through this map looking for the key that had the highest value.

    Declaring the map variable and initializing it with the input array will take 2-3 lines of code. Performing the search and returning the key with the highest found value will take another half-dozen lines of code.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Cool

    You do realize there is a much easier way to solve this problem rather than using containers?
    Mr. C: Author and Instructor

  4. #4
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Yeah, you can just use pointers...I think. I wish I knew how.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    You could also just use another array. but it could take up a bit more memory than needed. but for learning purposes, memory concerns are unnecessary at this point.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    Just have an array the size of the highest possible numbers....and then for each cell in the array, just increment array2[array1[i]], then find the max of the 2nd array

  7. #7
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    hmm, now i have something to do..im gonna see if i can do this.

    wouldn't sorting it be a good idea?
    Last edited by the Wookie; 05-14-2003 at 04:55 PM.

  8. #8
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    alright, it uses 2 vectors, this was the best i can come up with for now, im kinda tired lol.

    it just makes 2 parallel vectors, 1 holding the numbers, and one holding the counter. the main loop just searches the first list against itself, finding duplicates..

    so if the list is

    Code:
    5   2
    0   1 
    1   1 
    5   2
    the left column being the list of numbers, the right column being the number of those numbers found..like it found two 5s and one 0 and one 1. then the last loop just searches the right column for the largest value of n, and in a case like this it shouldnt matter if it picks the first 5 or the second, since both of their right column counts will be the same. i hope that makes sense

    i was also originally going to use arrays but i couldnt get them to cooperate, silly VC

    edit: ignore the "bool found;" you can just delete that, i was trying out a different method before i came up with this one
    Attached Files Attached Files
    Last edited by the Wookie; 05-14-2003 at 05:13 PM.

  9. #9
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    If any of you have the Deitel C++ How to Program text: Pages 278-282 has a program that finds the mean median and mode.

    Code:
    void mode(int freq[], int ans[], int size)
    not that hard!
    Mr. C: Author and Instructor

  10. #10
    tetra
    Guest
    or.... he could just do a switch statement.....


    if the mode is all your looking for....


    you could make counters for each number.

    and a switch something like this.

    Code:
    for(int i = 0; i < numOfEntries; i++)
    {
    
    switch (array[i])
    {
    case 1:
    1ctr++;
    break;
    
    case 2:
    1ctr++
    break;
    }
    
    // etc. etc. just have them enter a fixed range so you arent killing yourslef over switch statments...
    another way you could analize this... is with an adt.
    you could say.....

    class operation
    {
    int virtual sortNum(int []) = 0;
    }

    thats a long way and the switch serves a WAY easier solution but if you wanna impress the teacher do one of those

  11. #11
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    What if you had a lot of numbers - you could have a very large switch statement. The loop will do it with short code.
    Mr. C: Author and Instructor

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    This is what I was talking about:
    Code:
    #include <map>
    #include <iostream>
    using namespace std;
    
    int mode(int array[], int size)
    {
        // Create and load map container from input array
    
        map<int,int> IntMap;
        for( int a = 0; a < size; ++a ) IntMap[array[a]]++;
    
        // Find maximum value in map.
    
        map<int,int>::iterator it = IntMap.begin();
        int max = it->first;
        for( ; it != IntMap.end(); ++it )
            if( IntMap[it->first] > IntMap[max] ) max = it->first;
    
        return max;
    }
    
    int main()
    {
        int data[] = { 3, 4, 7, 2, 6, 9, 4, 8, 4 };
        cout << "Mode is " << mode(data,sizeof(data)/sizeof(data[0])) << endl;
        return 0;
    }
    Outputs: Mode is 4

    The map serves the same function as the arrays and vector containers mentioned by the Wookie and Jamsan. What's not easy about that?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13
    Shnakepup
    Guest
    Here's what i have so far. Please forgive any errors, unused methods, or just general idiocy. All i care about is functionality. After i get it to work, then i will go for finesse.

    Code:
    #include <iostream.h>
    
    void getnumbers(int *array);
    void sort(int *array);
    void modemedian(int *array, int &mode, int &median);
    void print(int *array, int &mode, int &median);
    
    int main()
    {
       int array[21];
       int mode, median;
       getnumbers(array);
       sort(array);
       modemedian(array, mode, median);
       print(array, mode, median);
       return 0;
    }
    
    void getnumbers(int *array)
    {
       int i;
       cout << "Enter 21 numbers. Hit enter for each one: ";
       for(i=0;i<21;i++)
            cin >> array[i];
    }
    
    void sort(int *array)
    {
       int temp, i, j;
       for(int i=0;i<20;i++)
       {
              for (int j=i + 1;j<21;j++)
              {
          	if (array[i] > array[j])
                    {
                           temp = array[i];
                           array[i]=array[j];
                           array[j]=temp;
                    }
              }
       }
    }
    
    void modemedian(int *array, int &mode, int &median)
    {
       int temp;
       int tempb = 0;
       int tempc = 0;
       int counters[21];
       median = array[10];
       //Mode section
       for(int i=0;i<21;i++)
       {
       	for(int j=0;j<21;j++)
       	{
       	         if (array[i]==array[j])
             	              counters[i]++;
                    }
       }
       cout << "Number of hits per number: ";   //I put this here just so i could see what the counter array was getting
       for(int i=0;i<21;i++)
              cout << counters[i] << " ";
       cout << endl;
       for(int i=0;i<21;i++)
       {
               if (counters[i]>tempb)
               {
          	tempb=counters[i];
                    tempc = i;
               }
       }
       mode=array[tempc];
       //end mode section
    }
    
    void print(int *array, int &mode, int &median)
    {
       cout << "You typed: ";
       for(int i=0;i<21;i++)
             cout << array[i] << " ";
       cout<< endl << "Mode: " << mode;
       cout<< endl << "Median: " << median;
    }
    Sorry if the logic's hard to follow...i'm a total beginner! The mode is never right, and when i check the counter array, it has all these huge numbers instead of the actual amount of hits per number. Can anyone decipher my work and tell me what i did wrong?

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    The basic logic looks OK but I do see a potential issue with the counters array not being initialized.
    Code:
    void modemedian(int *array, int &mode, int &median)
    {
        int temp;
        int tempb = 0;
        int tempc = 0; // Extra variable, not needed, see below
        int counters[21];
        median = array[10];
       
        //Mode section
        for(int i=0;i<21;i++)
        {
            counters[i] = 0; // Make sure you initialize the counter
                             // before attemtping to increment unknown values
            for(int j=0;j<21;j++)
            {
                if (array[i]==array[j])
                    counters[i]++;
            }
        }
       
        cout << "Number of hits per number: ";   //I put this here just so i could see what the counter array was getting
        //My compiler would have "issues" with using the same variable "i"
        // at this spot so I changed it
        for(int k=0;k<21;k++)
            cout << counters[k] << " "; 
        cout << endl;
        
        // Again, use of "i" variable here would cause me "issues"
        // with my compiler so I changed it.
        for(int l=0;l<21;l++)
        {
            if (counters[l]>tempb)
            {
                tempb=counters[l];
                //tempc = i; // Not needed...
                mode = array[l]; // Use this instead
            }
        }
        //mode=array[tempc]; // Not needed, replaced above
        //end mode section
    }
    Other than that, it looks like it should work.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  15. #15
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    This seems like the easiest solution:
    Code:
    int mode(int* array, int size)
    {
    	int largestNum = 0, largestCount = 0;
    
    	int* upperLimit = std::max_element(&array[0], &array[size]);
    	int* lowerLimit = std::min_element(&array[0], &array[size]);
    	for (int i = *lowerLimit; i <= *upperLimit; ++i)
    	{
    		int currentCount = std::count(&array[0], &array[size], i);
    		if (currentCount > largestCount)
    		{
    			largestCount = currentCount;
    			largestNum = i;
    		}
    	}
    	return largestNum;
    }
    Last edited by Eibro; 05-15-2003 at 03:08 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding largest number in array
    By el_chupacabra in forum C Programming
    Replies: 2
    Last Post: 01-22-2009, 01:31 PM
  2. Finding array dimensions
    By deviousdexter in forum C# Programming
    Replies: 3
    Last Post: 11-12-2008, 09:34 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Finding a hyphen in an array.
    By omnificient in forum C Programming
    Replies: 7
    Last Post: 06-17-2008, 02:31 AM
  5. Finding largest element in array
    By Chaplin27 in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2005, 09:18 PM

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