0

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[0];
double min = a[0];
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[100]; //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;
}
```

*Edited 3 Years Ago by Reverend Jim*: Fixed formatting