I cannot figure out number 3 and 4. Median and standard deviation. I think i am close but not getting the right answer. Any ideas? Thanks.

``````/*      37 April 2011
Assingment:      Project 2 I revision 1,2,5,6,7 and comments and formatinng done.. need 3,4
Description:     Implement a menu system managing an arry of data
*/

#include <stdio.h>
#include <math.h>

// Calculate sum and mean
void mean(double a[],int n)
{
int i;                //setting variables for local function use
double sum=0;
double avg=0;

for(i=0 ; i<n ;i++)    //finding sum and average
{
sum=sum+a[i];         // sum math
avg=sum/n;            // average math
}

printf("Your sum is %lf\n", sum); //displaying sum
printf("Mean= %lf\n", avg);       //displaying average
}

//Calculate median
void median(double a[], int n)
{
int i;
double x,even,y,odd;  //setting variables for local function use
x=0;
x=n;

if (n%2==0) //calculating even median
{
even=x/2;

printf(" Your median is %lf ", even);  // Printing even median
}
else        //calculating odd median
{
odd = (a[i/2] + a[i/2+1])/2;
printf("Your median is %lf ", odd);    // Printing odd median
}
}

//  Calculate the standard deviation
void dev(double a[], int n)
{

double sum = 0;           // The sum of all elements so far.
double sum_sqrs = 0;      // The sum of their squares.
double variance, average,std_dev;
int i;

for (i = 0; i < n; i++)
{
sum += a[i];
sum_sqrs += a[i]*a[i];
}

//Compute the average and variance,using the equality: Var(X) = E(X^2) - E(X)*E(X)
average = (double)sum / (double)n;
variance = (double)sum_sqrs / (double)n - (average)*(average);

// Compute the standard deviation.
std_dev = sqrt(variance);

printf("The deviation is %lf", variance);
}
//calculate range

void range(double a[],int n)
{
int i;
double range;     //setting variables for local function use
double max = a;
double min = a;

for (i = 0; i < n; i++)   // using loop than calculating max and min
{
if (a[i] > max)
{
max = a[i];
}
else if (a[i] < min)
{
min = a[i];
}
}
range=max-min;     //calculating range
printf ("The range of the array is: %lf\n", range); //printing range
}

//Calculate Frequency

void freq(double a[],int n)
{

double count_neg=0;  //setting variables for local function use
double count_1=0;
double count_2=0;
double count_3=0;
int i;

for (i = 0; i < n; i++)  // using for loop for couting
{
if(a[i]<0)              // counting for less than zero
{
count_neg++;
}
if(a[i]>0 && a[i]<10)   //counting for greater than zero and less than 10
{
count_1++;
}
if(a[i]>10 && a[i]<100) //counting for greater than tenn and less than 100
{
count_2++;
}
if(a[i]>100)            //counting for greater than 100
{
count_3++;
}
}
printf("(negative infinity,0)   = %lf\n",count_neg);
printf("(0,10)                  = %lf\n",count_1);
printf("(10,100)                = %lf\n",count_2);
printf("(100,positive infinity) = %lf\n",count_3);
}

int main(void)
{

int num7=7, opt;    // setting variables

printf("Hello!\n");
printf("This program will let you input the size and then data for an array.\n");
printf("Feel free to make the array as large or as small as you would like.\n");
printf("You will then be given several options to choose from.\n");
printf("Each option will give you a chance to go back to the main\n");
printf("menu unless you exit the program.\n");
printf("Enjoy!!!!\n\n\n\n\n");

int i,k,res;
int n, c; //size of array
double a; //array variable

printf("Enter the size of your Array: "); //asking user for array size
scanf("%d", &n);                          //getting size from user

for(i=0; i<n; i++)
{
printf("Enter Integer: ");  //input array
scanf("%lf", &a[i]);         //getining array data from user.

}

printf("\nPlease select from one of the following operations:\n\n");
printf("1. Display data set\n");
printf("2. Calculate the mean and sum of data set\n");
printf("3. Calculate the median of the data set\n");
printf("4. Calculate the standard deviation of the data set\n");
printf("5. Find the range of the data set\n");
printf("6. Display a frequency distribution\n");
printf("7. Quit\n");

while(num7=7)       //Setting While loop
{
printf("\nSelect Option:  "); //asking user for option
scanf("%d", &opt);  // Takes in the option

if (opt==7)
{
return 0;
}

switch(opt)    // switch set up to look for the option variable
{
/*  OPTION #1 */

case 1: //if user choses option 1

printf("Your oringal array is: ");   //displaying original array for user

for(i=0;i<n;i++)  //Setting loop for display
{
printf("%lf ", a[i]); //printing array on screen for user
}

break;

/* OPTION #2 */

case 2: //if user chooses option 2

mean(a,n);  // calling mean function

break;

/* OPTION #3 */

case 3: //if user chooses option 3

median(a,n); //calling median function

break;

/* OPTION #4 */

case 4: //if user chooses option 4

dev(a,n);  // calling deviation function

break;

/* OPTION #5 */

case 5: //if user choose option 5

range(a,n); //calling range function

break;

/* OPTION #6 */

case 6:

freq(a,n);  //calling frequency function

break;

/* OPTION #7  will quit the program*/

/* If there is an incorrect option chosen */

default: printf("Invaild response. Please choose from one of the seven choices. Thank you!");

}
}

getch();
return 0;

}
``````

## All 5 Replies

First, use code tags around your program's code. Nobody wants to study code that looks like the dog's dinner, two days ago. ;)

Second, narrow it down. WHAT do you need help with? Are you getting the mean calculated OK?

Narrow down what we should focus on, and save us all a lot of time. You'd be amazed at the pure genius we see in noobs, with messing up a program!

And no matter how badly you want to avoid it, you WILL have to learn some troubleshooting techniques - and practice them. Your programs will initially have bugs, and you will have to hunt them down.

So what about the mean? Is that good?

Sorry it looks a lot cleaner on dev-c. The mean calculation works fine. It is the median and standard deviation calculations that are not giving me the correct answers. For example when I calculate standard deviation for an array ( 1 2 3 4 5 ) it should be 1.5 but it is computing 2.0. When I run the median calculation the dos windows errors out and quits. I did not get any compiler errors. So the two main codes would be:

``````//Calculate median
void median(double a[], int n)
{
int i;
double x,even,y,odd;  //setting variables for local function use
x=0;
x=n;

if (n%2==0) //calculating even median
{
even=x/2;

printf(" Your median is %lf ", even);  // Printing even median
}
else        //calculating odd median
{
odd = (a[i/2] + a[i/2+1])/2;
printf("Your median is %lf ", odd);    // Printing odd median
}
}

//  Calculate the standard deviation
void dev(double a[], int n)
{

double sum = 0;           // The sum of all elements so far.
double sum_sqrs = 0;      // The sum of their squares.
double variance, average,std_dev;
int i;

for (i = 0; i < n; i++)
{
sum += a[i];
sum_sqrs += a[i]*a[i];
}

//Compute the average and variance,using the equality: Var(X) = E(X^2) - E(X)*E(X)
average = (double)sum / (double)n;
variance = (double)sum_sqrs / (double)n - (average)*(average);

// Compute the standard deviation.
std_dev = sqrt(variance);

printf("The deviation is %lf", variance);
}``````

In the function to calculate the median you have used the variable i without initializing it.

I can't believe I did not set it to i=zero for median.. It worked as soon as I made the change.. Thank you.. i am a dummy.. Still need help with my deviation if someone can see another stupid mistake.. I did troubleshoot my code for a couple of days before I asked for help..

In the function to calculate variance is
(a) Average being calculated correctly ?
(b) Can you please use brackets to more explicitly specify the order of the arithmetic operations

When you write

``variance = (double)sum_sqrs / (double)n - (average)*(average);``

It will
(1) divide sum_sqrs by n
(2) multiple average by average
Subtract (2) from (1)

Is this what you want ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.