should have posted the full code, sorry bout that tabstop.. yes i did set it initially to 0 but not when found a new mode... hmmm, here is the full code anyway... going to play with it.
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
float arraymode[100]; // array of type float with 100 elements to store mode values
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
int c, n, p, x; // used in for loop to find mode
float mode; // the mode
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;
c = 0;
p = 0;
x = 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);
// mean
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);
// standard deviation
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);
// variance
var = ( sumcalc/((float)k) );
printf("The variance of the data stored is: %f\n", var);
// sort array
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;
}
}
}
//median
if (k%2 == 1) {
m = (k/2);
med = array[m];
printf("The median of the data stored is: %f\n", med);
}
else {
m = (k/2);
med = (( array[m-1] + array[m] ) / 2);
printf("The median of the data stored is: %f\n", med);
}
//mode
for ( i = 0; i < (k-1); ++i ) {
if (array[i] == array[i+1]) {
++c;
}
else {
c = 0;
continue;
}
if (c > p) {
mode = array[i];
}
if ( (c > 0) && (c == p) ) {
arraymode[x] = array[i];
++x;
}
p = c;
}
printf("The mode of the data stored is: %f, ", mode);
for ( i = 0; i < x; ++i) {
printf("%f, ", arraymode[x]);
}
printf(".\n");
}
} while (j != 3);
return 0;
}