Code:
//// main.c
// statistics3
//
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXLINES 10
int compare (const void * a, const void * b);
//calculate mean prototype
float calculate_mean(float function_data[],int number_of_data);
int main (void)
{
int counter, number_of_data, printer_counter=1;
float data[MAXLINES], standard_deviation_array[MAXLINES];
int array_index=0;
int x;
int menu_selection = 0;
int program_continue = 1;
float mean, standard_deviation, variance, median, mode;
//int input = 0;
//char c;
while (program_continue == 1)
{
printf("This program will perform the following:\n1. Enter data.\n2. Display the data and the following statistics: the number of data items, the high and low values in the data, the mean, median, mode, variance and standard deviation.\n3. Quit the Program\n");
printf("select 1,2 or 3\n");
//menu_selection = getchar();
scanf("%d",&menu_selection);
while(getchar() != '\n');
switch (menu_selection)
{
case 1:
counter=1;
printf("You have selcted #1. Please enter d when done. Could not get EOF to work.\n");
while ( (array_index <= MAXLINES-1) )
{
printf("enter data line #%d:",counter);
scanf(" %f",&data[array_index]);
array_index++;
counter++;
if ( ( x=getchar() ) == 'd')
{
array_index = MAXLINES+1;
}
}
/*if (counter > MAXLINES)
printf("\n\nNumber of data lines: %d\n\n\n",counter-1);
if (counter < MAXLINES)
printf("\n\nNumber of data lines: %d\n\n\n",counter-2);*/
array_index = 0;
break;
case 2:
//printing the number of data lines:
if (counter > MAXLINES)
printf("\n\nNumber of data lines: %d\n\n\n",counter-1);
if (counter < MAXLINES)
printf("\n\nNumber of data lines: %d\n\n\n",counter-2);
number_of_data=counter;
while ( (array_index < MAXLINES) || printer_counter < counter)
{
printf("data line #%d:",printer_counter);
printf("%f\n",data[array_index]);
array_index++;
printer_counter++;
}
array_index = 0;
printer_counter = 1;
printf("\n");
int index=0;
float sum=0;
//Calculating the mean:
//mean=calculate_mean(data,number_of_data);
while (index<(number_of_data))
{
mean+=data[index];
index++;
}
mean/=(number_of_data-1);
printf("\n\nThe mean: %f\n\n",mean);
//Calculating the standard deviation:
index=0;
while (index<(number_of_data-1))
{
//subtract the mean from every number to get the list of deviations. Next, square the resulting list of numbers:
standard_deviation_array[index]=powf((data[index]-mean), 2);
//Add up all of the resulting squares to get their total sum:
sum+=standard_deviation_array[index];
index++;
}
variance=sum;
//Calculating the variance:
variance/=index;
//Divide the result by one less than the number of items in the list.
//To get the standard deviation, just take the square root of the resulting number
standard_deviation=sqrt(sum/(number_of_data-1));
printf("\n\nThe standard deviation: %f\n\n",standard_deviation);
printf("\n\nThe variance: %f\n\n",variance);
//finding the median
//First, sort the numbers from lowest to highest
qsort(data, MAXLINES, sizeof(float), compare);
//then find the middle number
//if the number of data items is odd, it is the middle number.
if (index%2 != 0)
{
median = data[(index/2)];
}
//But if the number of items is even, take the average of the two middle numbers.
if (index%2 == 0)
{
median = (data[ (index/2) ]+data[ (index/2)+2 ])/2;
}
printf("\n\nThe median: %f\n\n",median);
//Calculating the MODE:
//find the most frequently occuring number:
//start with the first cell , and compare to second, then third...compare to all the other cells
int index2=0, index3=0, mode_counter[MAXLINES];
//index is equal to the number of data items:
while (index2 < index )
{
while (index3 < index )
{
if (data[index2]==data[index3])
{
mode_counter[index2]++;
}
index3++;
}
index2++;
index3=0;
}
//mode_couner array should now contain a tally of the number of occurences of each number in data array.
index2=0;
index3=0;
printf("mode tally:\n");
printf("%d\n",mode_counter[0]);
printf("%d\n",mode_counter[1]);
printf("%d\n",mode_counter[2]);
printf("%d\n",mode_counter[3]);
printf("%d\n",mode_counter[4]);
printf("%d\n",mode_counter[5]);
printf("%d\n",mode_counter[6]);
printf("%d\n",mode_counter[7]);
printf("%d\n",mode_counter[8]);
printf("%d\n",mode_counter[9]);
break;
case 3:
printf("goodbye!");
program_continue = 0;
break;
default: printf("That is not a valid entry.\n");
}
}
return 0;
}
//calculating the mean
float calculate_mean(float function_data[],int number_of_data)
{
int index;
float total=0;
for(index=0;index<=number_of_data;index++)
total=total+function_data[index];
return (total/number_of_data);
}
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}