0

I cant Use ARRAY. I have to use only one variable. And calculate the median. i use following code to enter 10 nos(Unsorted)

Printf("enter 10 nos");
for(i=0;i<=10;i++)
scanf("%d",&a);

I tried but can only find the minimum and maximum.but no luck with middle most no.

D77pak
  • 21
  • 3
  • 1
    possible duplicate of [How to calculate or approximate the median of a list without storing the list](http://stackoverflow.com/questions/638030/how-to-calculate-or-approximate-the-median-of-a-list-without-storing-the-list) – unwind Nov 27 '13 at 15:19
  • Live on Earth without breathing!! – haccks Nov 27 '13 at 15:19
  • i cant use any thing like list ,array,stack just 1 variable for 10 nos...so retaining it is a great problem – D77pak Nov 27 '13 at 15:22
  • The restriction on having only a single variable is not practical. Use as many variables as you want. That's what they're for. And since the array is so small, you may as well sort it. (Besides, your code above uses two variables already, so the one variable restriction is already being ignored.) – Raymond Chen Nov 27 '13 at 15:23
  • i want to use many variable but my TEACHER said it has a possible solution using a single variable – D77pak Nov 27 '13 at 15:24
  • 1
    @user2873941 do you mean three variables? loop counter, input from user and accumulator... – Jekyll Nov 27 '13 at 15:26
  • @user2873941. may be your teacher looks for a mathematic demonstration that kind of operation is impossible – MOHAMED Nov 27 '13 at 15:26
  • Would you post the solution you came up with for the "minimum" problem. I would like to clearly understand what really are your restrictions – manuell Nov 27 '13 at 15:28
  • I'm guessing that you didn't understand your teacher when she said "single variable"... – quazzieclodo Nov 27 '13 at 15:30
  • i have to use a single variable to store the input but i can use other variable for running the loop,storing minimum and maximum and other counter variable – D77pak Nov 27 '13 at 15:34
  • "median" means `sum/count`? As `count` is 10 in your problem, you need only a variable for `sum` which is updated in your loop (`sum += a`). – ensc Nov 27 '13 at 15:57
  • @ensc That's mean, not median. – quazzieclodo Nov 27 '13 at 15:58
  • @Jekyll That's a good point. I hadn't thought of that... – quazzieclodo Nov 27 '13 at 16:04
  • 3
    Are sure your teacher is asking for the *median* and not the *mean*? – John Bode Nov 27 '13 at 16:09
  • YES she asked for median. – D77pak Nov 27 '13 at 16:10
  • good question @JohnBode – Jekyll Nov 27 '13 at 16:11
  • Let me say you are at the second number and you have 4 and 6, there is only one variable to store the value. Which will you store there? That's a real problem, because the other variable will be lost and if that was the median now it's gone, you won't remember you had a 4. – Jekyll Nov 27 '13 at 16:12
  • i will store 4 in min..min=4 and 6 in max..max=4 – D77pak Nov 27 '13 at 16:14
  • i cant use different variables to store different numbers as the user enters – D77pak Nov 27 '13 at 16:15
  • You can't use an array... Can you use a linked list or a heap? – quazzieclodo Nov 27 '13 at 16:16
  • 1
    You can't compute a median without storing at least half of the values... You misunderstood. – quazzieclodo Nov 27 '13 at 16:28
  • The median of _10_ numbers cannot, in general, be expected to be 1 of the 10 numbers. It needs to be the "number" between the 5th and 6th number. Therefore _some_ calculation on the numbers needs to be had. Note: adding bot INT_MIN and INT_MAX to the list does not change the answer - not sure if this helps. BTW: What is the range of allowable numbers to enter? – chux - Reinstate Monica Nov 27 '13 at 18:34
  • @chux any 10 numbers i.e. integer type – D77pak Nov 28 '13 at 08:30
  • possible duplicate of [C program to find the median of of 10 numbers without using array](http://stackoverflow.com/questions/20245540/c-program-to-find-the-median-of-of-10-numbers-without-using-array) – Joe Nov 28 '13 at 15:12
  • BTW: `for(i=0;i<=10;i++)` iterates 11 times. Suggest `for(i = 0; i < 10; i++)`. – chux - Reinstate Monica Dec 01 '13 at 12:08
  • Don't forget to share the answer with us. – Raymond Chen Dec 03 '13 at 14:59
  • possible duplicate of [Finding the median of an unsorted array](http://stackoverflow.com/questions/10662013/finding-the-median-of-an-unsorted-array) – Joseph Quinsey Jan 19 '14 at 22:41

1 Answers1

0

It's not easy since to do it on one traversal without prior sorting but it can be done.

You need a way of calculating the running median.

One approach is to use an order statistic tree. See http://pine.cs.yale.edu/pinewiki/OrderStatisticsTree. That's probably the single variable that your teacher is talking about. But it's not easy to coerce into a single variable in C.

But it's not a sensible way of getting the median statistic. The normal route is to build an interpolator representing a quantile function and use that to extract the median.

Bathsheba
  • 220,365
  • 33
  • 331
  • 451
  • The order statistic tree can be used if the inputs are random. That's the whole point. Did you read the link? – Bathsheba Nov 27 '13 at 16:28