Mode of an array

This is a discussion on Mode of an array within the C Programming forums, part of the General Programming Boards category; My problem is I have an array filled with some values. I need to find the mode (the number that ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    63

    Question Mode of an array

    My problem is I have an array filled with some values. I need to find the mode (the number that occurs most often) of the array. I've been thinking about it for a while but no idea, maybe use a for loop to help count if an array's values equal each other. You don't have to write the code but if you could at least give me some idea on how it should look. I've already gone through finding the mean, median, ect... here is my code, no attempt at writing mode yet.

    Code:
    /*
    
    My name is Jack Trocinski
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
    	float array[200]; // array of type float with 200 elements used for user input
    	float arraycalc[200]; // array of type float with 200 elements used for calculations
    	int i; // used in for loop
    	int j; // used in menu
    	int k; // used to count number of elements in array
    	int l; // used in nested for loop to sort array
    	int m; // used to find the median
    	float med; // the median
    	float a; // used in nested for loop to store array value
    	float summ; // the sum associated with the mean
    	float sumcalc; // the sum associated with sd and var
    	float mean; // the mean
    	float sd; // the standard deviation
    	float var; // the variance
    	k = 0;
    	
    	do {
    		printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
    		printf("@                              @\n");
    		printf("@ 1. Enter data                @\n");
    		printf("@ 2. Display data & statistics @\n");
    		printf("@ 3. Exit                      @\n");
    		printf("@                              @\n");
    		printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n");
    		
    		scanf("%d", &j);
    
    			if (j == 1) {
    				for ( i = k; i < 200; ++i ) {
    					printf("Please input your data and press enter, use CTRL-Z to when you're done: ");
    					k = i;
    					if ( scanf("%f", &array[i]) != 1 ) break;
    				}
    			}
    			
    			else if (j == 2) {
    			
    				summ = 0;
    				sumcalc = 0;
    				
    				printf("The total number of items stored in data is: %d\n", k);
    				
    				for ( i = 0; i < k; ++i ) {
    					summ = array[i] + summ;
    					mean = ( summ/((float)k) );
    				}
    				
    				printf("The mean of the data stored is: %f\n", mean);
    				
    				for ( i = 0; i < k; ++i ) {
    					arraycalc[i] = pow( (array[i] - mean), 2 );
    				}
    				
    				for ( i = 0; i < k; ++i) {
    					sumcalc = arraycalc[i] + sumcalc;
    					sd = pow( (sumcalc/((float)k) ), 0.5 );
    				}
    				
    				printf("The standard deviation of the data stored is: %f\n", sd);
    				
    				var = ( sumcalc/((float)k) );
    				printf("The variance of the data stored is: %f\n", var);
    				
    				for ( i = 0; i < k; ++i) {
    					for ( l = 0; l < (k-1) ; ++l ) {
    						if ( array[l] > array[l+1] ) {
    							a = array[l];
    							array[l] = array[l+1];
    							array[l+1] = a;
    						}
    					}
    				}
    				
    				if (k%2 == 1) {
    					m = (k/2);
    					med = array[m];
    					printf("The median is: %f\n", med);
    				}
    				else {
    					m = (k/2);
    					med = (( array[m-1] + array[m] ) / 2);
    					printf("The median is: %f\n", med);
    				}
    				
    				
    				
    			}
    			
    	} while (j != 3);
    
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you know the possible range of values in the array, and it is small, you can count them. Otherwise you can sort the array and look for a large block of the same number.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    No I don't know the range of the values. I already sorted the array in ascending order, OH I see what you mean. Ok, I'm going to try working on that.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Also, I guess this is more of a math question let's say if we have the data set 1 , 3, 3, 5, 5

    Are 3 and 5 both the mode? If so how can I account for that.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,459
    I think you take the mean (average) of all the modes. Check Wikipedia or mathforum.org to be sure.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    updated code, now when i execute the code it stops after it displays the median data stored... I commented as Mode... also not sure if my mode code is correct. let me know what you guys think. There are no compile errors or warnings.

    Code:
    /*
    
    My name is Jack Trocinski
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
    	float array[200]; // array of type float with 200 elements used for user input
    	float arraycalc[200]; // array of type float with 200 elements used for calculations
    	int i; // used in for loop
    	int j; // used in menu
    	int k; // used to count number of elements in array
    	int l; // used in nested for loop to sort array
    	int m; // used to find the median
    	int c, max, num; // used in for loop to find mode
    	float mode; // the mode
    	float med; // the median
    	float a; // used in nested for loop to store array value
    	float summ; // the sum associated with the mean
    	float sumcalc; // the sum associated with sd and var
    	float mean; // the mean
    	float sd; // the standard deviation
    	float var; // the variance
    	k = 0;
    	c = 0;
    	max = 0;
    	num = 0;
    	
    	do {
    		printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
    		printf("@                              @\n");
    		printf("@ 1. Enter data                @\n");
    		printf("@ 2. Display data & statistics @\n");
    		printf("@ 3. Exit                      @\n");
    		printf("@                              @\n");
    		printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n");
    		
    		scanf("%d", &j);
    
    			if (j == 1) {
    				for ( i = k; i < 200; ++i ) {
    					printf("Please input your data and press enter, use CTRL-Z to when you're done: ");
    					k = i;
    					if ( scanf("%f", &array[i]) != 1 ) break;
    				}
    			}
    			
    			else if (j == 2) {
    			
    				summ = 0;
    				sumcalc = 0;
    				
    				printf("The total number of items stored in data is: %d\n", k);
    				
    				for ( i = 0; i < k; ++i ) {
    					summ = array[i] + summ;
    					mean = ( summ/((float)k) );
    				}
    				
    				printf("The mean of the data stored is: %f\n", mean);
    				
    				for ( i = 0; i < k; ++i ) {
    					arraycalc[i] = pow( (array[i] - mean), 2 );
    				}
    				
    				for ( i = 0; i < k; ++i) {
    					sumcalc = arraycalc[i] + sumcalc;
    					sd = pow( (sumcalc/((float)k) ), 0.5 );
    				}
    				
    				printf("The standard deviation of the data stored is: %f\n", sd);
    				
    				var = ( sumcalc/((float)k) );
    				printf("The variance of the data stored is: %f\n", var);
    				
    				for ( i = 0; i < k; ++i) {
    					for ( l = 0; l < (k-1) ; ++l ) {
    						if ( array[l] > array[l+1] ) {
    							a = array[l];
    							array[l] = array[l+1];
    							array[l+1] = a;
    						}
    					}
    				}
    				
    				if (k%2 == 1) {
    					m = (k/2);
    					med = array[m];
    					printf("The median of the data stored is: %f\n", med);
    				}
    				else {
    					m = (k/2);
    					med = (( array[m-1] + array[m] ) / 2);
    					printf("The median of the data stored is: %f\n", med);
    				}
    				
    				//mode
    				
    				for ( i = 0; i < k; ++i ) {
    					if (array[i] == array[i+1]) {
    						++c;
    					}
    					
    					else {
    						num = array[i];
    						c = 0;
    					}
    					
    					if (c > max) {
    						max = c;
    						mode = num;
    					}				
    				}
    				
    				printf("The mode of the data stored is: %f\n", mode);
    				
    			}
    			
    	} while (j != 3);
    
    	return 0;
    }

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I think you might be setting c to 0 a bit too soon. In any rate, I would put that c>max check inside the else statement anyway (no reason to do it except when you switch numbers, right?).

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,254
    Quote Originally Posted by yacek View Post
    Also, I guess this is more of a math question let's say if we have the data set 1 , 3, 3, 5, 5

    Are 3 and 5 both the mode? If so how can I account for that.
    3 and 5 are both modes. The data is bimodal.

    Similarly, if all values in the data are unique (none occurs more than once) then the data has no mode.
    Right 98% of the time, and don't care about the other 3%.

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Yep, so I ended up getting this code... but it only works if the set has one mode. How can I get it to work if it has more than that. Look at the code under the comment 'mode'

    Code:
    /*
    
    My name is Jack Trocinski
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
    	float array[200]; // array of type float with 200 elements used for user input
    	float arraycalc[200]; // array of type float with 200 elements used for calculations
    	int i; // used in for loop
    	int j; // used in menu
    	int k; // used to count number of elements in array
    	int l; // used in nested for loop to sort array
    	int m; // used to find the median
    	int c, n, p; // used in for loop to find mode
    	float mode; // the mode
    	float med; // the median
    	float a; // used in nested for loop to store array value
    	float summ; // the sum associated with the mean
    	float sumcalc; // the sum associated with sd and var
    	float mean; // the mean
    	float sd; // the standard deviation
    	float var; // the variance
    	k = 0;
    	c = 0;
    	p = 0;
    	
    	
    	do {
    		printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
    		printf("@                              @\n");
    		printf("@ 1. Enter data                @\n");
    		printf("@ 2. Display data & statistics @\n");
    		printf("@ 3. Exit                      @\n");
    		printf("@                              @\n");
    		printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n");
    		
    		scanf("%d", &j);
    
    			if (j == 1) {
    				for ( i = k; i < 200; ++i ) {
    					printf("Please input your data and press enter, use CTRL-Z to when you're done: ");
    					k = i;
    					if ( scanf("%f", &array[i]) != 1 ) break;
    				}
    			}
    			
    			else if (j == 2) {
    			
    				summ = 0;
    				sumcalc = 0;
    				
    				printf("The total number of items stored in data is: %d\n", k);
    				
    				// mean
    				
    				for ( i = 0; i < k; ++i ) {
    					summ = array[i] + summ;
    					mean = ( summ/((float)k) );
    				}
    				
    				printf("The mean of the data stored is: %f\n", mean);
    				
    				// standard deviation
    				
    				for ( i = 0; i < k; ++i ) {
    					arraycalc[i] = pow( (array[i] - mean), 2 );
    				}
    				
    				for ( i = 0; i < k; ++i) {
    					sumcalc = arraycalc[i] + sumcalc;
    					sd = pow( (sumcalc/((float)k) ), 0.5 );
    				}
    				
    				printf("The standard deviation of the data stored is: %f\n", sd);
    				
    				// variance
    				
    				var = ( sumcalc/((float)k) );
    				printf("The variance of the data stored is: %f\n", var);
    				
    				// sort array
    				
    				for ( i = 0; i < k; ++i) {
    					for ( l = 0; l < (k-1) ; ++l ) {
    						if ( array[l] > array[l+1] ) {
    							a = array[l];
    							array[l] = array[l+1];
    							array[l+1] = a;
    						}
    					}
    				}
    				
    				//median
    				
    				if (k%2 == 1) {
    					m = (k/2);
    					med = array[m];
    					printf("The median of the data stored is: %f\n", med);
    				}
    				else {
    					m = (k/2);
    					med = (( array[m-1] + array[m] ) / 2);
    					printf("The median of the data stored is: %f\n", med);
    				}
    				
    				//mode
    				
    				for ( i = 0; i < (k-1); ++i ) {
    					if (array[i] == array[i+1]) {
    						++c;
    					}
    					
    					else {
    						c = 0;
    					}
    					
    					if (c > p) {
    						p = c;
    						mode = array[i];
    					}
    					
    					
    				}
    				
    				printf("The mode of the data stored is: %f\n", mode);
    				print
    				
    			}
    			
    	} while (j != 3);
    
    	return 0;
    }

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Ok you guys im almost there, got the mode to work if there is a single mode in the set now I'm trying to get it to work if there are two or more modes in the set... when i enter a set like 2, 2, 3, 3, 4 I get the mode to be 2.000000, 0.000000 ... why do i always get a 0 being printed out. no warnings/errors during compile.

    mode code

    Code:
    //mode
    				
    				for ( i = 0; i < (k-1); ++i ) {
    					if (array[i] == array[i+1]) {
    						++c;
    					}
    					
    					else {
    						c = 0;
    						continue;
    					}
    					
    					if (c > p) {
    						mode = array[i];
    					}
    					
    					if ( (c > 0) && (c == p) ) {
    						arraymode[x] = array[i];
    						++x;
    					}
    					p = c;
    				}
    				
    				printf("The mode of the data stored is: %f, ", mode);
    				for ( i = 0; i < x; ++i) {
    					printf("%f, ", arraymode[x]);
    				}
    Last edited by yacek; 12-10-2010 at 09:03 PM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    At some point you need to set x = 0 (i.e., at the beginning, and when you find a new mode). Also you're printing arraymode[x] which is never set.

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    should have posted the full code, sorry bout that tabstop.. yes i did set it initially to 0 but not when found a new mode... hmmm, here is the full code anyway... going to play with it.

    Code:
    /*
    
    My name is Jack Trocinski
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
    	float array[200]; // array of type float with 200 elements used for user input
    	float arraycalc[200]; // array of type float with 200 elements used for calculations
    	float arraymode[100]; // array of type float with 100 elements to store mode values
    	int i; // used in for loop
    	int j; // used in menu
    	int k; // used to count number of elements in array
    	int l; // used in nested for loop to sort array
    	int m; // used to find the median
    	int c, n, p, x; // used in for loop to find mode
    	float mode; // the mode
    	float med; // the median
    	float a; // used in nested for loop to store array value
    	float summ; // the sum associated with the mean
    	float sumcalc; // the sum associated with sd and var
    	float mean; // the mean
    	float sd; // the standard deviation
    	float var; // the variance
    	k = 0;
    	c = 0;
    	p = 0;
    	x = 0;
    	
    	
    	do {
    		printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
    		printf("@                              @\n");
    		printf("@ 1. Enter data                @\n");
    		printf("@ 2. Display data & statistics @\n");
    		printf("@ 3. Exit                      @\n");
    		printf("@                              @\n");
    		printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n");
    		
    		scanf("%d", &j);
    
    			if (j == 1) {
    				for ( i = k; i < 200; ++i ) {
    					printf("Please input your data and press enter, use CTRL-Z to when you're done: ");
    					k = i;
    					if ( scanf("%f", &array[i]) != 1 ) break;
    				}
    			}
    			
    			else if (j == 2) {
    			
    				summ = 0;
    				sumcalc = 0;
    				
    				printf("The total number of items stored in data is: %d\n", k);
    				
    				// mean
    				
    				for ( i = 0; i < k; ++i ) {
    					summ = array[i] + summ;
    					mean = ( summ/((float)k) );
    				}
    				
    				printf("The mean of the data stored is: %f\n", mean);
    				
    				// standard deviation
    				
    				for ( i = 0; i < k; ++i ) {
    					arraycalc[i] = pow( (array[i] - mean), 2 );
    				}
    				
    				for ( i = 0; i < k; ++i) {
    					sumcalc = arraycalc[i] + sumcalc;
    					sd = pow( (sumcalc/((float)k) ), 0.5 );
    				}
    				
    				printf("The standard deviation of the data stored is: %f\n", sd);
    				
    				// variance
    				
    				var = ( sumcalc/((float)k) );
    				printf("The variance of the data stored is: %f\n", var);
    				
    				// sort array
    				
    				for ( i = 0; i < k; ++i) {
    					for ( l = 0; l < (k-1) ; ++l ) {
    						if ( array[l] > array[l+1] ) {
    							a = array[l];
    							array[l] = array[l+1];
    							array[l+1] = a;
    						}
    					}
    				}
    				
    				//median
    				
    				if (k%2 == 1) {
    					m = (k/2);
    					med = array[m];
    					printf("The median of the data stored is: %f\n", med);
    				}
    				else {
    					m = (k/2);
    					med = (( array[m-1] + array[m] ) / 2);
    					printf("The median of the data stored is: %f\n", med);
    				}
    				
    				//mode
    				
    				for ( i = 0; i < (k-1); ++i ) {
    					if (array[i] == array[i+1]) {
    						++c;
    					}
    					
    					else {
    						c = 0;
    						continue;
    					}
    					
    					if (c > p) {
    						mode = array[i];
    					}
    					
    					if ( (c > 0) && (c == p) ) {
    						arraymode[x] = array[i];
    						++x;
    					}
    					p = c;
    				}
    				
    				printf("The mode of the data stored is: %f, ", mode);
    				for ( i = 0; i < x; ++i) {
    					printf("%f, ", arraymode[x]);
    				}
    				
    				printf(".\n");
    				
    			}
    			
    	} while (j != 3);
    
    	return 0;
    }

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    WOW, finally got it to work with this code... code for the mode, for anyone doing this problem in the future obviously you need to declare everything properly in main:

    Code:
    for ( i = 0; i < (k-1); ++i ) {
    					if (array[i] == array[i+1]) { ++c; }
    										
    					else { c = 0; }
    					
    					if ( (c > 0) && (c == p) ) {
    						printf("%f, and ", array[i]);
    					}
    					
    					if (c > p) {
    						mode = array[i];
    						p = c;
    					}
    					
    				}
    				printf("%f. \n", mode);
    			}

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    This code will calculate the mode for a set of data in any range, and will return multiple modes if the set has such. For example the set: 1, 1, 2, 2, 3, 3, 4 will return you modes: 1, 2, 3 since they occur most often in the set and each equally of often as the other. k represents the amount of elements in the array, I used k-1 since for an array of 200 elements you wouldn't want to compare element 200 with element 201 since it doesn't exist.
    Last edited by yacek; 12-10-2010 at 10:19 PM.

  15. #15
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    might be some bugs though still... :/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. warning: excess elements in array initializer
    By redruby147 in forum C Programming
    Replies: 6
    Last Post: 09-30-2009, 06:08 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  4. Array help
    By deedlit in forum C Programming
    Replies: 4
    Last Post: 11-05-2003, 09:55 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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