C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-27-2008, 10:12 AM   #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;

  }
  }
afiq8289 is offline   Reply With Quote
Old 11-27-2008, 10:51 AM   #2
Registered User
 
Join Date: Sep 2006
Posts: 2,512
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.
Adak is offline   Reply With Quote
Reply

Tags
c programming, mode

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:06 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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