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;
}
}