Thread: help with sorting of array and median!

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    help with sorting of array and median!

    I have to write a code that uses functions

    I have to calculate the median of an array that is inputted by the user,
    therefore I have to design a function that sorts the numbers.

    So in my median function i need to call on the sort function to get the numbers in order than to determine the median from there to give back to the MAIN program...


    I dont even know where to start any help?

    this is what I have so far
    from the median function and the sort function i am making for my code


    +++++
    Code:
    #include <stdio.h>
    #include <math.h>
    
    double median(double a[], int n);
    void sort(double a[], int n);
    
    int main (void)
    {
         int i, w;
         med;
    
    
         printf("Enter the number of elements in the array: ");
         scanf("%d", &w);
    
         double a[w];
    
         for(i=0; i <w; i++)
             {
                 printf("Enter element %d: ", i);
                 scanf("%lf", &a[i]);
              }
    
          med = median(a,w);
    
          printf("The median is: %.2f", med);
     
    
          return 0;
    }
    
    double median(double a[], int n)
    {
        int i=0;
        for(i=0; i<n; i++)
             {
                 b[i] = a[i];
              }
    
         return median;
    
    }
    
    
    void sort(double a[], int n)
    {
             int middle;
    }
    Last edited by Cali_engr; 04-05-2010 at 11:21 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    AFTER you have sorted your array median will be (if I remember correctly):
    Code:
    double median(double* arr, size_t size)
    {
       return arr[size/2];
    }
    so now you only need to sort array.

    there is qsort standard function that could help with this task
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculations on an entered array
    By MV1 in forum C Programming
    Replies: 7
    Last Post: 03-10-2009, 10:28 AM
  2. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  3. Median
    By Neo1 in forum C++ Programming
    Replies: 4
    Last Post: 07-02-2007, 04:00 PM
  4. moving median function
    By supermeew in forum C Programming
    Replies: 0
    Last Post: 05-04-2006, 02:37 PM
  5. computing median value
    By ademkiv in forum C Programming
    Replies: 3
    Last Post: 04-03-2006, 09:43 PM