My problem is I have an array filled with some values. I need to find the mode (the number that occurs most often) of the array. I've been thinking about it for a while but no idea, maybe use a for loop to help count if an array's values equal each other. You don't have to write the code but if you could at least give me some idea on how it should look. I've already gone through finding the mean, median, ect... here is my code, no attempt at writing mode yet.
Code:
/*
My name is Jack Trocinski
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float array[200]; // array of type float with 200 elements used for user input
float arraycalc[200]; // array of type float with 200 elements used for calculations
int i; // used in for loop
int j; // used in menu
int k; // used to count number of elements in array
int l; // used in nested for loop to sort array
int m; // used to find the median
float med; // the median
float a; // used in nested for loop to store array value
float summ; // the sum associated with the mean
float sumcalc; // the sum associated with sd and var
float mean; // the mean
float sd; // the standard deviation
float var; // the variance
k = 0;
do {
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf("@ @\n");
printf("@ 1. Enter data @\n");
printf("@ 2. Display data & statistics @\n");
printf("@ 3. Exit @\n");
printf("@ @\n");
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n");
scanf("%d", &j);
if (j == 1) {
for ( i = k; i < 200; ++i ) {
printf("Please input your data and press enter, use CTRL-Z to when you're done: ");
k = i;
if ( scanf("%f", &array[i]) != 1 ) break;
}
}
else if (j == 2) {
summ = 0;
sumcalc = 0;
printf("The total number of items stored in data is: %d\n", k);
for ( i = 0; i < k; ++i ) {
summ = array[i] + summ;
mean = ( summ/((float)k) );
}
printf("The mean of the data stored is: %f\n", mean);
for ( i = 0; i < k; ++i ) {
arraycalc[i] = pow( (array[i] - mean), 2 );
}
for ( i = 0; i < k; ++i) {
sumcalc = arraycalc[i] + sumcalc;
sd = pow( (sumcalc/((float)k) ), 0.5 );
}
printf("The standard deviation of the data stored is: %f\n", sd);
var = ( sumcalc/((float)k) );
printf("The variance of the data stored is: %f\n", var);
for ( i = 0; i < k; ++i) {
for ( l = 0; l < (k-1) ; ++l ) {
if ( array[l] > array[l+1] ) {
a = array[l];
array[l] = array[l+1];
array[l+1] = a;
}
}
}
if (k%2 == 1) {
m = (k/2);
med = array[m];
printf("The median is: %f\n", med);
}
else {
m = (k/2);
med = (( array[m-1] + array[m] ) / 2);
printf("The median is: %f\n", med);
}
}
} while (j != 3);
return 0;
}