0

I am creating a program which performs statistical analysis of an array. So far i have got an enter array, that gives the min, max and median values and sorts the array. I have not been able to work out the mode. I have been able to find the mode on hardcoded array without the other analysis. Could anyone help me get the mode to work please.

This is the program as is now.

```
#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 : \nTo end and 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--;
}
printf("Sorted List: \n");
// 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;
}
for(i = 0; i < sizeof a / sizeof a[0]; i++) /* for each datum */
{
++freq[a[i]]; /* track the frequency */
if(freq[a[i]] > mode) /* is this now the most common item? */
{
mode = a[i]; /* 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<< */
//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]));
}
```

This is the code that can get the mode of an entered array.

```
#include <stdio.h> /* prototype for printf, and def of size_t */
#define MINVAL 0 /* the lowest allowable data value */
#define MAXVAL 42 /* the highest allowable data value */
int main(void)
{
int data[] = { 0, 1, 2, 3, 4, 5, 3, 7 }; /* initialise the data array */
int freq[MAXVAL + 1 - MINVAL] = { 0 }; /* clear the frequency
* counts to 0.
*/
size_t i = 0; /* loop counter */
int mode = 0; /* for recording the modal value */
for(i = 0; i < sizeof data / sizeof data[0]; i++) /* for each datum */
{
++freq[data[i]]; /* track the frequency */
if(freq[data[i]] > mode) /* is this now the most common item? */
{
mode = data[i]; /* 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<< */
return 0;
}
```

Your help would be greatly appriciated, i am eager to learn how to accomplish this.