![]() |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 2
| Finding mode! 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 | |
| | #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;
}
}
}
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];
}
|
| Adak is offline | |
![]() |
| Tags |
| c programming, mode |
| Thread Tools | |
| Display Modes | |
|
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 |