Median of array values
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Median of array values

  1. #1
    Join Date
    Jan 2005
    Posts
    317

    Median of array values

    Hi All,

    If i have 8-bit values saved in an array of 127 bytes, how would i calculate the Median of this array. Median of a set of values would be the central value of the set when its values are arranged in ascending order.

    Any help would be great.

    -naim1

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: Median of array values

    What part don't you understand?

    Is this C++?

    http://www.cplusplus.com/reference/algorithm/sort/

  3. #3
    Join Date
    Jan 2005
    Posts
    317

    Re: Median of array values

    Thanks Chris F for the link.

    Is it possible to do it using plain ANSI C ?

  4. #4
    Join Date
    Aug 2008
    Posts
    902

    Re: Median of array values

    I've never used this before, but here is a C library function.

    http://www.cplusplus.com/reference/c...cstdlib/qsort/

  5. #5
    Join Date
    Jan 2005
    Posts
    317

    Re: Median of array values

    Thanks again Chris F.

    any idea where can i get code for qsort()?

    Another point is that the values are not constant in the array, instead the array is being filled up constatly by newer values which are bering received from the serial port. Is it possible to do the sorting operation of every new incoming value dynamically, something like serial-arithmatic?

  6. #6
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Median of array values

    Quote Originally Posted by naim1 View Post
    Thanks again Chris F.

    any idea where can i get code for qsort()?
    The qsort() is a standard library function. Any code for it would come with your compiler (if your compiler offers source code to the library routines).

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,196

    Re: Median of array values

    If you wanted a better average case performance than sorting and then picking the middle, you could use the nth element algorithm of picking a pivot, partitioning as in quick sort, and then only performing the recursion on the partition that contains the middle element.

    Quote Originally Posted by naim1
    Another point is that the values are not constant in the array, instead the array is being filled up constatly by newer values which are bering received from the serial port. Is it possible to do the sorting operation of every new incoming value dynamically, something like serial-arithmatic?
    As in the new values replace existing ones in the array?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center