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.
Printable View
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.
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.
Hallo knik 653!
May be you did mean something like this below?
It may be useful for exampleCode:#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++;
}
}
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.