Mode of an array in C

Reply

Join Date: Sep 2009
Posts: 1
Reputation: willingj is an unknown quantity at this point 
Solved Threads: 0
willingj willingj is offline Offline
Newbie Poster

Mode of an array in C

 
0
  #1
Sep 2nd, 2009
I am continuing work on a program for analysis of an entered array, I have got min,max,median and sorting the array working with some input parameters. I cannot seem to get the mode to work, i have attempted some code to do this. Your help would be greatly appriciated, i am eager to learn how to accomplish these things.
Code:


#include <stdio.h>
#include <stdlib.h>

#define N 100
#define MINVAL 0 /* the lowest allowable data value */
#define MAXVAL 100 /* the highest allowable data value */

int main()
{
int a[N] = {0};
int i, j, value, n;

int freq[MAXVAL + 1 - MINVAL] = { 0 }; /* clear the frequency counts to 0. */
size_t k = 0; /* loop counter */
int mode = 0; /* for recording the modal value */

printf("Please enter array: To sort Array enter 0 \n");

// Upto 100 items can be created in a[i]
scanf("%d", &a[0]);
for(i = 1; i < N && a[i-1] < 100 && a[i-1] != 0; i++)
{
// Input of the numbers into array a[i]
scanf("%d", &a[i]);
}
n = i;

if(a[n-1] > 100)
{
printf("Number must be between 0 and 100");
exit(1);
}

if (a[n-1] == 0)
{
a[n-1] = 0;
n--;
}


for(k = 0; k < sizeof a / sizeof a[0]; k++) /* for each datum */
{
++freq[a[k]]; /* track the frequency */
if(freq[a[k]] > mode) /* is this now the most common item? */
{
mode = a[k]; /* yes, so log it */
}
}
printf("The modal value is %d which occurs %d time%s\n",
mode,
freq[mode],
freq[mode] == 1 ? "" : "s"); /* 1 time, 2 time>>s<< */

printf("Sorted List: ");

// Simple insertion sort
for(i = 1; i < n; i++)
{
value = a[i];
for (j = i - 1; j >= 0 && a[j] > value; j--)
a[j + 1] = a[j];
a[j + 1] = value;
}

//Prints the sorted array
for(i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
printf("Min: %d\n", a[0]);
printf("Max: %d\n", a[n-1]);
printf("Median: %f\n", n % 2 ? a[n/2] : 0.5*(a[n/2] + a[n/2-1]));
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: Mode of an array in C

 
0
  #2
Sep 2nd, 2009
What you want from us to do from this crapy code. No code tags, no comments.

Please put code tags and comments in your code.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

Re: Mode of an array in C

 
0
  #3
Sep 2nd, 2009
use two fors for sort i=0 to n-1 j=i+1 to n
Last edited by akulkarni; Sep 2nd, 2009 at 5:50 pm.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC