computing median value

This is a discussion on computing median value within the C Programming forums, part of the General Programming Boards category; Hi, I wrote a program that is using arrays that calculates sum, and avegare of the number user inputs (maximum ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    Philadelphia, PA
    Posts
    27

    computing median value

    Hi,

    I wrote a program that is using arrays that calculates sum, and avegare of the number user inputs (maximum 20 numbers). Now I'm trying to write one more function that sorts the array, and another function that computes median. I don't even know where to start with these functions. Please help me figure out the sorting function that sorts in ascending order.

    As for the median function, I think that I could write a code that finds out whether the array has even or odd number of values and then if odd, just take the middle value; else, add two middle values and divide them by 2.

    Here's my code so far,

    Code:
    #include<stdio.h>
    
    /*Function prototypes*/
    int getdata(double[],int size);
    double computesum(int n, double x[]);
    double computemean(int n, double sum);
    
    int main(void)
    {
            double x[20];
    int size=0;
    int n;
    double sum;
    /*Function calls */
            n=getdata(x, 20);
            computesum(n, x);
            computemean(n, sum);
    }
    
    /*This function gets data from the user */
    int getdata(double x[],int size)
    {
    //int x[20];
    //int size=0
    int tmp;
    int flag=0;
    int n=0;
    char cond, y;
       while(n<size && flag == 0)
        {
            printf("Enter a number\n");
            scanf("%d", &tmp);
            printf("Are you done? Press Y if yes, otherwise press N\n");
            scanf("%c", &y);
            scanf("%c", &cond);
            if (cond == 'n'){
                flag=0;
            }
            else if(cond =='y')
            }
            else if(cond =='y')
                    flag=1;
            x[n]=tmp;
            n = n+1;
       }
            return(n);
    
    
    }
    
    double computesum(int n, double x[])
    {
    //int j;
    //for (j=0; j<n; j++) printf("%f", x[j]);
    int i;
    double tmp;
    tmp=0;
    i=0;
    int size;
    
    while(i<n)
      {
            tmp += x[i];
            i = i+1;
      }
    
            printf("Sum of the values entered is %f\n",tmp);
    
            return(tmp);
    }
    
    double computemean(int n, double sum)
    {
    
    double mean, tmp;
    
    mean=tmp/n;
            printf("Mean of the values entered is %f\n",mean);
    }
    Thanks

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    For sorting you could try implementing your function using something simple like the bubblesort algorithm.
    For calculating the median, get ahold of the size of the array first then return the value of the middle element if the array size isn't divisable by 2 and return the mean of the two elements that share the place of being in the middle otherwise.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Location
    Philadelphia, PA
    Posts
    27
    Here's my code with the sorting function implemented.

    Code:
    #include<stdio.h>
    
    /*Function prototypes*/
    int getdata(double[],int size);
    double computesum(int n, double x[]);
    double computemean(int n, double sum);
    double computemedian(double x[], int n);
    
    int main(void)
    {
    
            double x[20];
    
    int size=0;
    int n;
    double sum;
    
            n=getdata(x, 20);
            computesum(n, x);
            computemean(n, sum);
            double median = computemedian(x, n);
    
    }
    
    int getdata(double x[],int size)
    {
    //int x[20];
    //int size=0
    int tmp;
    int flag=0;
    int n=0;
    char cond, y;
    
       while(n<size && flag == 0)
        {
            printf("Enter a number\n");
            scanf("%d", &tmp);
            printf("Are you done? Press Y if yes, otherwise press N\n");
            scanf("%c", &y);
            scanf("%c", &cond);
            if (cond == 'n'){
                flag=0;
    
            }
            else if(cond =='y')
                    flag=1;
            x[n]=tmp;
            n = n+1;
       }
            return(n);
    
    
    }
    
    double computesum(int n, double x[])
    {
    //int j;
    //for (j=0; j<n; j++) printf("%f", x[j]);
    int i;
    double tmp;
    tmp=0;
    i=0;
    int size;
    
    while(i<n)
      {
            tmp += x[i];
            i = i+1;
      }
    
            printf("Sum of the values entered is %f\n",tmp);
    
            return(tmp);
    }
    double computemean(int n, double sum)
    {
    double mean, tmp;
    mean=tmp/n;
            printf("Mean of the values entered is %f\n",mean);
    }
    
    double computemedian(double x[], int n)
    {
            int i;          /*Array elements*/
            int j;
            int k;
            int temp;       /*Temporary storage for some array element*/
            double median;
    printf("before sorting, the array looks like:\n");
    for (j=0; j<n; j++) printf("%lf\n", x[j]);
    for (j = n; j>0; j = j-1)
            {
                    k = 0;
                    for (i=1; i<=j; i = i+1)
                            if (x[i]>x[k])
                                    k = i;
                    temp = x[k];
                    x[k] = x[j];
                    x[j] = temp;
            }
    
    printf("after sorting, the array looks like:\n");
    for (j=0; j<n+1; j++) printf("%lf\n", x[j]);
    
    if (n%2 == 0)
    {
            median = (x[(n/2)-1] + x[(n/2)+1]) / 2;
    }
    else
    {
            median = x[n/2];
    }
            printf("The median of all the input values is %lf\n", median);
    }
    The code compiles, but for some reason, after sorting my array is getting enlarged by one digit:

    This is what i get before and after sorting.
    Code:
    Enter a number
    7
    Are you done? Press Y if yes, otherwise press N
    n
    Enter a number
    3
    Are you done? Press Y if yes, otherwise press N
    n
    Enter a number
    7
    Are you done? Press Y if yes, otherwise press N
    y
    Sum of the values entered is 17.000000
    Mean of the values entered is 5.666667
    before sorting, the array looks like:
    7.000000
    3.000000
    7.000000
    after sorting, the array looks like:
    0.000000
    3.000000
    7.000000
    7.000000
    The median of all the input values is 3.000000
    Any idea how i can preserve the size of the array?? That 0.00000 is not supposed to be there.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Location
    Philadelphia, PA
    Posts
    27
    doesn't any one know how to fix this problem?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Median filter help
    By JTEK24 in forum Tech Board
    Replies: 10
    Last Post: 07-16-2009, 06:05 PM
  2. moving median function
    By supermeew in forum C Programming
    Replies: 0
    Last Post: 05-04-2006, 02:37 PM
  3. small Distributed computing utilies
    By vinit in forum C Programming
    Replies: 2
    Last Post: 04-17-2006, 03:37 AM
  4. Computing Mean, Median and Mode Using Arrays
    By Rodneo in forum C++ Programming
    Replies: 0
    Last Post: 05-29-2002, 11:40 PM
  5. median
    By frank in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2001, 03:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21