Thread: Finding mode!

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    Finding mode!

    I am encountering problem to find mode in my program.. Can anyone fix this? Thanks!


    Code:
    #include<stdio.h>
    #include<conio.h>
    #define SIZE 5
    void main()            /*Declaration of the main function*/
      {
        int a[50],i,j,k,m,z,temp,median;
        float s=0.00,mean, mode;
        printf("Enter the integers :-\n");
        for(i=0;i<SIZE;i++)
          scanf("\n %d",&a[i]);
     
           for(i=0;i<SIZE-1;i++)
    	 for(j=0;j<SIZE-1-i;j++)
    	   if(a[j]<a[j+1])
    	     {
    	       temp=a[j];
    	       a[j]=a[j+1];
    	       a[j+1]=temp;
    	     }
    	     
    	     	/*Finding highest and lowest and mode*/
    	double prevNumber = a[0] - 1;
        int timesUsed, mostUsed = -1;
        for ( i = 0; i < SIZE; i++ ) {
            if (prevNumber == a[i]) {
                timesUsed++;
            } else {
                prevNumber = a[i];
                timesUsed = 1;
            }
          {
          	 if (timesUsed > mostUsed)
    			mostUsed = timesUsed;
                mode = a[i];
            	printf("\n\n%s\n%s\n%s\n%s", "***********", "   Mode", "***********");
             	printf("\n\tMode=%d\n", mode);
            }
            printf("\n%s\n%s\n%s\n%s", "****************************", "   Lowest and Highest Value", "****************************");
             printf("\n\tHighest=%d\n", a[0]);
             printf("\tLowest=%d\n",a[SIZE-1]);
             
    	     
    	  /*Printing the sorted array*/
    	     printf("\n The number in sorted order is :=\n");
    	       for(i=0;i<SIZE;i++)
    		{
    		 printf("\n%d",a[i]);
    		 s=s+a[i];
    		}
    		
    			  /*Finding mean*/
    		printf ("\n\n%s\n%s\n%s\n", "***********", "   Mean", "***********");	  
    		printf ("\n\tTotal=%f",s);
    		mean=s/SIZE;
    		printf("\n\tMean=%f",mean);
    
    	     /*Finding median*/
    	     printf("\n\n%s\n%s\n%s\n%s", "***********", "   Median", "***********");
    		if((SIZE%2)==0)
    		  printf("\n\tMedian=%d\n",a[(SIZE/2)-1]);
    		 else
    		 printf("\n\tMedian=%d\n",a[((SIZE+1)/2)-1]);
    
    
    
    return 0;
    
      }
      }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,866
    I'm not familiar with your sorting logic. Is there a particular reason not to use this simple selection sort?

    Code:
    for(i = 0; i < SIZE - 1; i++)   {
       for(j = i + 1; j < SIZE; j++)   {
          if(array[i] > array[j]   {
             temp = array[i];
             array[i] = array[j];
             array[j] = temp;
          }
       }  
    }
    I have never seen a simpler sorting block of code, that could run any faster. Once you start mucking around with sorting code, it's easy to simply break the logic. It may no longer sort correctly, or it may simply take longer to sort than it should. To confound matters worse, it may sort one set of data correctly, but not sort all sets of data correctly.

    In your mode code, are you saving the value of the most common digit, as you go through the array and check each number?

    Code:
    next = highest =  repeat = mode = 0;
    for each number in the array   {
       if(array[number] == last_number)   {
          repeat++;
           if(repeat > highest)   {
              highest = repeat;
              mode = array[number];
          }
       }
       else   //the number was not repeated
           repeat = 0;
       last_number = array[number];
    }
    I haven't tried this, but it should get started on the right path. Do verify your sorting is working properly though. The above will require a sorted array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding mode in array of numbers
    By Snowcat in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2006, 09:58 PM
  2. finding mode of an array
    By n00by in forum C Programming
    Replies: 1
    Last Post: 05-18-2004, 07:40 PM
  3. Finding the Mode of an integer array
    By -]SC[-Dargoth in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 02:27 PM
  4. Finding the mode of an array
    By Shnakepup in forum C++ Programming
    Replies: 16
    Last Post: 05-16-2003, 10:01 AM
  5. Finding Mode Median and Mean
    By Ginny Morgan in forum C Programming
    Replies: 3
    Last Post: 05-08-2003, 03:09 PM

Tags for this Thread