Thread: finding the mode in an array

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    finding the mode in an array

    I've been at this for a few hours now. I've been trying to find the mode in the const data array. Here is my code so far
    Code:
    static float modeOfValues(const float *data,int count){
    int meanCounter = 0;
    int i,j;
    float mode;
    int counter = 1;
    int countArray[200];
    	for(i = 0;i < count;i++)//initialize the count array to all zeros
    		countArray[i]=0;
    
    	for(i=0;i<count;i++){
    		for(j=i+1;j<count;j++){
    			if(data[i]==data[j]){
    				countArray[i]=counter+1;
    				printf("counter%d\n",counter);
    			}
    		}
    	}
    	for(i=0;i<count;i++)
    		printf("%d\n",countArray[i]);	
    	
    	for (i=0;i<count;i++){
    		if(countArray[i] > meanCounter){
    			meanCounter = data[i];
    		}
    		else if(countArray[i] < meanCounter){
    			meanCounter = data[i];
    		}	
    	}
    	mode = data[meanCounter];
    	return mode;
    }
    I'm trying to create an array that will hold how many times a number comes up in the original array, but the numbers in the counter array are not getting incremented the way I hope they do. Can anyone see something I can't ?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Counting generally involves adding one to the number you have (countArray) not some completely other number (counter).

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    I thought that countArray[i] + 1 would just increment the index to the next position. I am trying to increment the value in countArray when I come up to a value in the original array where there are similar values in the array.

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, 02:31 PM
  2. Finding array dimensions
    By deviousdexter in forum C# Programming
    Replies: 3
    Last Post: 11-12-2008, 10: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. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM