Thread: Trying to calculate a running average

  1. #16
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    It's dead simple. Look how I did it in my code. An array and a loop. That's it. You don't need any of the extra machinery on offer here.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  2. #17
    Registered User
    Join Date
    Oct 2011
    Posts
    44
    Quote Originally Posted by john.c View Post
    It's dead simple. Look how I did it in my code. An array and a loop. That's it. You don't need any of the extra machinery on offer here.
    I apologize for not getting it. Based on your code, how do you know which value is the oldest to replace?

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by knik653 View Post
    I apologize for not getting it. Based on your code, how do you know which value is the oldest to replace?
    This question makes you think that there is an actual decision being made here. There is not. If you read the numbers and stored them in order 1-5, then by definition 1 is the oldest and 5 is the newest; so you replace them in the same order you read them in, which means you don't actually do anything different but read them in, in order 1-5 just like you did the first time.

  4. #19
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66
    Hallo knik 653!

    May be you did mean something like this below?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /** running average berechnet waehrend der Eingabe der Werte den Mittelwert
      * und errechnet ihn nach jeder Eingabe neu */
    
    
    #define max_n 5
    
    double Input_Values(void);
    double calc_sum(double werte[]);
    void InitTemp(double (*werte)[max_n]);
    void show_values(double werte[]);
    void calc_average(void);
    
    
    int main()
    {
     printf("Running average\n");
     calc_average();
    
     return 0;
    }
    
    double Input_Values(void)
    {
     double d = 0.0;
     printf("Enter a double: ");
     scanf("%lf", &d);
    
     return d;
    }
    
    double calc_sum(double werte[])
    {
     int i;
     double sum = 0.0;
    
     for (i = 0; i < max_n; i++)
      sum += werte[i];
    
     return sum;
    }
    
    void InitTemp(double (*werte)[max_n])
    {
     int i;
     for (i = 0; i < max_n; i++)
      (*werte)[i] = 0;
    }
    
    void show_values(double werte[])
    {
     printf("Values of temps: temp[0]=%.3lf   temp[1]=%.3lf   temp[2]=%.3lf  temp[3]=%.3lf   temp[4]=%.3lf\n", werte[0],  werte[1], werte[2], werte[3], werte[4]);
    }
    
    void calc_average(void)
    {
     double temps[max_n];
     double sum = 0.0;
     double average = 0.0;
     int slei, i, n;
    
     i = 0;
     n = 1;
    
     InitTemp(&temps);
    
     for (slei = 0; slei < 10; slei++)
      {
       temps[i] = Input_Values();
       sum = calc_sum(temps);
       average = sum / n;
       printf("i:%d    n:%d    sum:%.3lf    arithmetically averaged: %.3lf\n", i, n, sum, average);
       show_values(temps);
       i++;
       if(i >= max_n) i = 0;
       if(n < max_n) n++;
      }
    
    }
    It may be useful for example
    to calculate the standard deviation of Parts how made with a lathe machine,
    or a milling machine,
    to use it for Shewhart individuals control charts
    or anything.

    Years ago i was skilled worker for quality assurance.

    I hope, my example can help you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to calculate the average of scores?
    By david.jones in forum C Programming
    Replies: 4
    Last Post: 05-02-2011, 06:33 AM
  2. Replies: 26
    Last Post: 08-19-2009, 07:59 AM
  3. calculate average
    By archriku in forum C Programming
    Replies: 23
    Last Post: 04-10-2009, 04:27 AM
  4. calculate average from a file
    By mrsirpoopsalot in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2009, 02:25 PM

Tags for this Thread