shadiestone
asked on
troubled code - finding the mean, median, mode, standard deviation, variance of an array
here is my code ( it sux)
i got the mean, im working on the median, i have no clue as to what the standard deviation or variance means, but one small step at a time i guess...if you can help with any of these it would be much appreciated
#include <stdio.h>
#define MAX 20
void print_inventory( double inventory[], int numitems); //print out the data
int inputinventory( double inventory[], int maxnum); //input the data function
void print_avg( double inventory[], int numitems); //print the average function
void swap( double *a, double *b);
void bubble_sort( double data[], int num_elts );
//void median( double elements[], int num_elts) //find the median fuction
int main( void )
{
double inventory[MAX];
int num_items;
printf( "Please enter the number of items in stock.");
printf( "enter -1 when you are done.\n");
num_items = input_inventory( inventory, MAX );
print_inventory(inventory, num_items);
print_avg( inventory, num_items);
bubble_sort( inventory, num_items );
print_inventory(inventory, num_items);
//void median( double elements[], int num_elts)
return 0;
}
int input_inventory( double inventory[], int maxnum )
{
int index = 0;
scanf( "%lf", &inventory[ index ] );
printf("current input %lf:\n", inventory[index]);
while (index < maxnum-1 && inventory[ index ] != -1 ) {
index++;
scanf( "%lf", &inventory[index] );
printf("current input%lf:\n", inventory[index]);
}
if (index == maxnum - 1){
printf("No room for more items.\n" );
return (index+1);
}
else return ( index );
}
void print_inventory( double inventory[], int numitems)
{
int index;
for (index = 0; index < numitems; index++) {
printf("Item number %d:\t\t", index+1);
printf("Number on hand %lf\n", inventory[index] );
}
}
void print_avg( double inventory[], int numitems)
{
double sum=0;
int index;
for(index = 0; index < numitems ; index++){
sum += inventory[index];
}
printf("\nthe average of all items is %lf\n", sum / index);
}
void bubble_sort( double data[], int num_elts )
{
int i, j;
for ( i = 0; i < num_elts; i++ )
for ( j = num_elts-1; j > i; j--)
if ( data[j] < data[j-1] )
swap( &data[j], &data[j-1] );
}
void swap( double *a, double *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
/* troubled code */
void median( double inventory[], int num_elts)
{
int i;
for( i = 0; i < num_elts; i++;){
if(num_elts % 2 == 0)
printf("number of elements is even: %d", num_elts);
else
printf("number of elements is odd: %d", num_elts);
}
}
/*
here i was just trying to determine wether the number of elements was even or odd so i know whether i have to divide by two or not at all to find the median*/
i got the mean, im working on the median, i have no clue as to what the standard deviation or variance means, but one small step at a time i guess...if you can help with any of these it would be much appreciated
#include <stdio.h>
#define MAX 20
void print_inventory( double inventory[], int numitems); //print out the data
int inputinventory( double inventory[], int maxnum); //input the data function
void print_avg( double inventory[], int numitems); //print the average function
void swap( double *a, double *b);
void bubble_sort( double data[], int num_elts );
//void median( double elements[], int num_elts) //find the median fuction
int main( void )
{
double inventory[MAX];
int num_items;
printf( "Please enter the number of items in stock.");
printf( "enter -1 when you are done.\n");
num_items = input_inventory( inventory, MAX );
print_inventory(inventory,
print_avg( inventory, num_items);
bubble_sort( inventory, num_items );
print_inventory(inventory,
//void median( double elements[], int num_elts)
return 0;
}
int input_inventory( double inventory[], int maxnum )
{
int index = 0;
scanf( "%lf", &inventory[ index ] );
printf("current input %lf:\n", inventory[index]);
while (index < maxnum-1 && inventory[ index ] != -1 ) {
index++;
scanf( "%lf", &inventory[index] );
printf("current input%lf:\n", inventory[index]);
}
if (index == maxnum - 1){
printf("No room for more items.\n" );
return (index+1);
}
else return ( index );
}
void print_inventory( double inventory[], int numitems)
{
int index;
for (index = 0; index < numitems; index++) {
printf("Item number %d:\t\t", index+1);
printf("Number on hand %lf\n", inventory[index] );
}
}
void print_avg( double inventory[], int numitems)
{
double sum=0;
int index;
for(index = 0; index < numitems ; index++){
sum += inventory[index];
}
printf("\nthe average of all items is %lf\n", sum / index);
}
void bubble_sort( double data[], int num_elts )
{
int i, j;
for ( i = 0; i < num_elts; i++ )
for ( j = num_elts-1; j > i; j--)
if ( data[j] < data[j-1] )
swap( &data[j], &data[j-1] );
}
void swap( double *a, double *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
/* troubled code */
void median( double inventory[], int num_elts)
{
int i;
for( i = 0; i < num_elts; i++;){
if(num_elts % 2 == 0)
printf("number of elements is even: %d", num_elts);
else
printf("number of elements is odd: %d", num_elts);
}
}
/*
here i was just trying to determine wether the number of elements was even or odd so i know whether i have to divide by two or not at all to find the median*/
hi,
What does median do. you want to find the median of inventory or median of num_elts.
regards manish
What does median do. you want to find the median of inventory or median of num_elts.
regards manish
ASKER
inventory
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Inventory then,
/*median of inventry.*/
void median( double inventory[], int num_elts)
{
int i,;
double median = 0;
for( i = 0; i < num_elts; i++;){
median += inventry[i]
}
median = median/num_elts;
printf("the median is %f. ", median) ;
}
regards.
/*median of inventry.*/
void median( double inventory[], int num_elts)
{
int i,;
double median = 0;
for( i = 0; i < num_elts; i++;){
median += inventry[i]
}
median = median/num_elts;
printf("the median is %f. ", median) ;
}
regards.
Hi shadlestone,
@manish_regmi
> median = median/num_elts;
is not the median, but the average.
the median of a range of values is the value right in the middle of this values. there are as many values smaller than the median, as there are values bigger than it.
1 2 3 4 5 -> median 3
1 2 3 4 -> median (2+3)/2 = 2.5
double median(double inv[], int num)
{
if(num%2==0)
return (inv[num/2]+inv[1+(num/2)] )/2;
else
return inv[(num+1)/2];
}
i haven't checked if the array indices are correct, would be better to do it so.
important, the inv[] array has to be sorted to get the proper median value.
leflon
@manish_regmi
> median = median/num_elts;
is not the median, but the average.
the median of a range of values is the value right in the middle of this values. there are as many values smaller than the median, as there are values bigger than it.
1 2 3 4 5 -> median 3
1 2 3 4 -> median (2+3)/2 = 2.5
double median(double inv[], int num)
{
if(num%2==0)
return (inv[num/2]+inv[1+(num/2)]
else
return inv[(num+1)/2];
}
i haven't checked if the array indices are correct, would be better to do it so.
important, the inv[] array has to be sorted to get the proper median value.
leflon
Is this another test question? Smells like it anyway... ;) Better give hints to find the solution, or the bug, but not complete solutions.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Some are saying he needs to sort, he is already, right? Looks like levlon got it right. Just send the "num_elts" value for "num" variable in levlon's function, and you got it. shadie, in your code you don't need the for loop, since you know how many there are --> num_elts.
Within your bubble sort, quick way is is to call levlon's median function after the sort, and instead of returning median, print it. Not "elegant", since I think main() should recieve all values, but it works.
homework should be to look up std dev. and variance, and try those yourself.
Within your bubble sort, quick way is is to call levlon's median function after the sort, and instead of returning median, print it. Not "elegant", since I think main() should recieve all values, but it works.
homework should be to look up std dev. and variance, and try those yourself.
void median( double inventory[], int num_elts)
{
int i;
if(num_elts % 2 == 0)
printf("number of elements is even: %d", num_elts);
else
printf("number of elements is odd: %d", num_elts);
}
Why the for-loop?
Whatever statistical function you need, look in a statistics book or use Google to find the functions and their purpose explained.