median for 3 numbers in sequence

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
What is your knowledge of arrays and loops?
I know a little bit but not a lot. I can write the code to take a whole block of input numbers and then sorting them and then getting the median of that. My problem is to write the code that firstly takes the 3 numbers, do the magic and then tae numbers 2,3,4 and redo the process
Last edited on
If I understand correctly:
All you need is a for loop, that will of course start on the first element. The loop cannot go to the last - because that would be calculating the median of 19, 20 and 21. So you need the loop to go to 17. So, for 0= 1 to 17 (because you are starting from position 0)
{
output median of a[i], a[i+1], a[i+2]
}

Last edited on
yup that looks good. To confirm, yes you understood correctly, I need to calculate the median of the first, second and third number in the list of 20 and finish with the median of number 18, 19 and 20. That last 3 numbers (18,19,20) will print out the last median number and the whole program terminates
this is what I have so far


#include stdio.h
#define SIZE = 20;

void median(int answer[]);
void bubblesort(int a[]);
void printArray(const int a[])

int main(void)

int response[SIZE] =
{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}

median(response);
return 0;

void median(int answer[])
{
printf("\n%s\n%s\n%s\n%s"'
"*********, "Median", ******",
"The unsorted array of response is");
printArray(answer);
bubblesort(answer);

printf("\n\nThe sorted array is");
printArray(answer);
printf("\n\nThe median is element %d of\n"
"the sorted %d element array.\n"
"For this run the median is %d\n\n",
SIZE/2, SIZE, answer[SIZE/2]);

Now the for loop
Will it be something like:
for (i=1, i<SIZE, i++) but this only adjusts the whole loop by one... how do I tell the code to take the first 3 numbers and then increment by one?

I really appreciate all your help so far :)
What he was pointing out that you missed is that the quantity of numbers you have to process is not a multiple of 3.

That aside, I'd make a function:

double median_of(double d1, double d2, double d3)

define it, and go from there.
Last edited on
for i = 0 to 17 <---------NOTE THE 17!!!

a[i] a[i+1] a[i+2]

the loop starts with i=0, so that would be:
a[0] a[0+1] a[0+2]

if you have a median_of function as stated before, you call it with
median_of (a[i] , a[i+1] , a[i+2]);

this function accepts 3 numbers, and gets the median (if you have the sort algorithm, use that, just modify it to be for 3 numbers and so on...).

the function itself can print out the number, or return the value to be printed (hope you understand this).

so what you do before printing out you print out the "i", and you will get:
0 median, where i is 0 and median is the result of the function

that is the end of the loop, it starts over for i=1...
try to understand what i wrote above, and then think about what happens when i is 1
or even better take a paper and write it all down
ps about the 17 from the first line - you wrote 20...
having defined the above, how would the for loop look like?

If i simply say:
for (i=0, i<SIZE, i++)
the above will just run through all the numbers of i one at a time

then the second line should state

"take the first 3 numbers, find the median, print it out"

How do I write this in code. This is my main struggle at the moment
gotcha... thanks for that :)
simply just this? It didnt work because the median_of is not defined apparently...

#include <stdio.h>

int main()
{
int n[20] = {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};
int i;

printf("%s%13s\n", "Element", "Value");

for (i=0, i<20,i++);{
median_of (a[i], a[i+1], a[i+2]);
}

return 0;
I can get the listing of the numbers to work but I can still not get the for loop to work. Can somebody please elaborate on that a bit please?
Everything works up until the for loop comes in and then nothing works
simply just this? It didnt work because the median_of is not defined apparently.


I did say that you should define it.

1
2
for ( int i=0 ; i < 17 ; i+=3 )
   std::cout << median_of(n[i], n[i+1], n[i+2]) << '\n' ;


Again, you can't do i < 20 because the number of values you are dealing with is not a multiple of 3.

Notice the syntax of the for loop. There are no commas.
Last edited on
back again... still no good... im so lost at the moment.

#include <stdio.h>

int main()
{
int n[20] = {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};
int i;

for ( int i=0 ; i < 17 ; i+=3 )
std::cout << median_of(n[i], n[i+1], n[i+2]) << '\n' ;
return 0;
}

Should this not work???
No. You didn't define median_of.
it keeps saying I cant convert parameter from int to const char. Why so?
why are you putting i+3?
you want it to start at the first (i=0) to get n[0] n[1] n[2], then
i=0+1 , so i is 1, to get the median of n[1] n[2] n[3]

paste your median function
Topic archived. No new replies allowed.