Q9) In this question, you have to output the "two moving average" of a sequence of non-negative numbers.
The two moving average is the sequence of averages of the last 2 entries.
For the first number, no average is output.
For example, if the sequence of numbers is a1,a2,a3,a4,a5
The 2-moving average is (a1+a2)2,(a2+a3)2,(a3+a4)2,(a4+a5)2
.
Input
-------
The input is a sequence of non-negative floating point numbers, terminated by a -1.
The -1 is not part of the sequence. There will be at least 3 numbers in the sequence.
Output
----------
You have to output the moving average of the sequence. The output should be printed correct to one digit after the decimal.
Hint : Use the format specifier "%.1f" inside printf.
Sample Input 1
---------------------
1 3 2 4 -1
Sample Output 1
-------------------------
2.0 2.5 3.0
#include
void two_moving_average() {
float a, b, c;
scanf("%f", &a);
scanf("%f", &b);
while (1) {
scanf("%f", &c);
if (c == -1) {
break;
}
printf("%.1f\n", (a + b) / 2);
a = b;
b = c;
}
}
int main() {
two_moving_average();
return 0;
}