Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Find mean,median without using arrays

A fun problem.

The key to to find a place to store all the numbers. Code could use a file, a linked list, etc. The below uses a linked list and recursion.

Leave it to OP for refinements about multiple modes or median when count is even.

typedef struct num_S {
  struct num_S *prev;
  double x;
} num_T;

void GetNum(num_T *prev) {
  num_T current;
  current.prev = prev;
  // If new number found ...
  if (scanf("%lf", &current.x) == 1) {
    num_T *p = &current;
    // Sort new entry into list
    while (p->prev != NULL && p->x
< p->prev->x) {
      double t = p->x;
      p->x = p->prev->x;
      p->prev->x = t;
      p = p->prev;
  // End of list -now process the list
  } else {
    unsigned ModeCount = 0;
    double Mode = 0.0;
    unsigned ModeCandidateCount = 0;
    double ModeCandidate = 0.0 / 0.0;
    unsigned Count = 0;
    double SumX = 0.0;
    double SumXX = 0.0;
    num_T *p = current.prev;
    while (p != NULL) {
      SumX += p->x;
      SumXX += p->x * p->x;
      if (p->x == ModeCandidate) {
      } else {
        ModeCandidateCount = 1;
        ModeCandidate = p->x;
      if (ModeCandidateCount > ModeCount) {
        ModeCount = ModeCandidateCount;
        Mode = p->x;
      p = p->prev;

    printf("Count = %u
", Count);
    if (Count > 0) {
      printf("Mode = %lf  Count %u
", Mode, ModeCount);
      printf("Mean = %lf
", SumX / Count);
      printf("STD  = %lf
", sqrt(Count * SumX - SumXX) / Count);

      Count /= 2;
      num_T *p = current.prev;
      while (Count-- > 0) {
        p = p->prev;
      printf("Median = %lf
", p->x);

int main(void) {
  return 0;

Input 4 3 4 2 4 1 EOF

Count = 6
Mode = 4.000000  Count 3
Mean = 3.000000
STD  = 1.130388
Median = 3.000000

STD Ref: STD Rapid calculation methods

Categories : C

Related to : Find mean,median without using arrays
cannot convert ‘char*’ to ‘int*’ for argument ‘1’ to ‘int median(int*, int)’ array2 = median(array,size);
in the header int median(int array[],int size); in the .cpp file int median(char array[], int size) your main is trying to invoke the first one (its the only one it knows about)

Categories : C++
Median of multiple columns in MySql
By statistics, Median is the middle number in a given out distribution. For instance if in the column cat_id where you have value 1,2,3 etc. Your median is 2 since its the number or value at the middle. Query the middle value and then hurray. Give me a shout if you still need further guide. ..Sectona

Categories : Mysql
Haskell, How to define mean by recursion, median and variance?
You can define mean recursively in several ways, but the way I'm going to suggest minimizes the number of divisions necessary for efficiency: mean :: [Float] -> Float mean [] = error "Can't take mean of empty list" mean xs = go 0 0 xs where go len runningSum [] = runningSum / len go len runningSum (x:xs) = go (len + 1) (runningSum + x) xs Note that while an experience

Categories : Haskell
Draw gridlines at the average (or median) values of a range in d3.js
You'll want to calculate the mean and median based on your data, instead of your scale: ymean = d3.mean(inputData, function(d) { return d.xVal; }); xmean = d3.mean(inputData, function(d) { return d.yVal; }); These are generic accessor functions, if you need specific guidance on the accessor function (the function(d) { return d.xVal; } part), you'll need to provide the format of your input dat

Categories : Javascript
Statistical calculator- Median issue & Standard Deviation java
While the following is untested, they should work. For the median: public static double median(int[] numbers) { Arrays.sort(numbers); // sort numbers int len = numbers.length; if (len % 2 == 0) // length is even return (numbers[len/2] + numbers[len/2-1]) / 2.0d; else // length is odd return (double) numbers[len/2]; } Standard deviation is the mean

Categories : Java
Recently Add
Rewrite of IDA decompiled function
Computing the average of grades in C
passing structures to functions
Use semaphores for handling sockets in C
How to search for a string pattern inside html, coding in C?
sprintf invalid format '%d'
Why can't this c program run correctly?
Low Pass filter in C
Child process does not print anything
C: datatypes. sqrt function working with int why?
How to implement Serial Port Profile Link Command used in ConnectBlue Bluetooth Module?
Rerunning cancelled pthread
How to print out a string that is pointed to by a char pointer?
Can unverified scanf call cause an undefined behavior?
nested structures in C
Portable Makevars for R package using C, GSL and OpenMP with help of Rcpp
program doesn't run(getting RUN FINISHED Segmentation fault core dumped)
Finding the sign of a Lua number, from C
Width of symbols created by gcc's objectcopy
Strange behavior when using free in c program
Can't open image C
Strange C code - dynamic arrays?
C program printing weird characters
Fault in decimal to binary in c
How to detect broken pipe on input?
How can I ivestigate use of the register keyword in C?
File scaning with fscanf
why gcc generates "leal -8240(%ebp), %eax; cmpl $1, %eax; ja XXX" instead of jmp instruction?
Wierd Problems With My Own Custom Written Word Search Algorithm
Trouble with making a shell in C
© Copyright 2017 Publishing Limited. All rights reserved.