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}
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
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]
}
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
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?
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...
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