Hi all
I have a list of 20 numbers and I have to write a program that takes the first 3 numbers, calculate the median and shoots out the median in table format. Then the program takes numbers 2,3,4 and calculate the median and shoots it into a table. Then numbers 3,4,5 and so on until all 20 numbers have been used.
Any ideas how to implement this?

Here is a set of data for you to try your program out on:
{0.00, -0.07, 0.08, -0.01, 1.44, 0.00, 0.00, 0.07, -0.01, 0.00, 0.97,
0.96, 1.02, 0.93, 0.9, 0.24, 1.01, 1.04, 1.01, 1.02}

The correctly filtered data is:
{0.00, -0.07, 0.00, -0.01, 0.08, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
0.96, 0.97, 0.96, 0.93, 0.90, 0.90, 1.01, 1.01, 1.02}

Time step Input Moving window Output
0 0.00 ( 0.00, , ) 0.00
1 -0.07 (-0.07, 0.00, ) -0.07
2 0.08 ( 0.08, -0.07, 0.00) 0.00
3 -0.01 ( -0.01, 0.08, -0.07) -0.01

Etc....
Thank in advance

Use a Vector as your starting point to calculate the median. Next you want to get the size of the vector. Store the size in a temp variable for later use. Now you want to partition the vector using Standard Template Library (STL) nth_element. Partition should be midpoint based upon the size retrieved previously. The median value is now located at the mid point index in the vector.

Put the above code in a function and feed it three successive variables at a time. Finally, print the output from the function used to calculate the median

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.