943,701 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1004
  • C RSS
Sep 2nd, 2009
0

Mode of an array in C

Expand Post »
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]));
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
willingj is offline Offline
1 posts
since Sep 2009
Sep 2nd, 2009
0

Re: Mode of an array in C

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.
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
Sep 2nd, 2009
0

Re: Mode of an array in C

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.
Reputation Points: 11
Solved Threads: 4
Junior Poster
akulkarni is offline Offline
111 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: A couple of questions on C
Next Thread in C Forum Timeline: assigning data to a 2d char array within structure





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC