Assignment 3_Question 2:
In this question, you have to output the "moving average" of a
sequence of non-negative numbers. The 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
then 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.
Sample Input 1
-------------------------------------
1 2 3 -1
Sample Output 1
-------------------------
1.5 2.5
PROGRAM:
#include<stdio.h>
int main()
{
float a[100],b[100];int i=-1,n=0;
float m;
do
{
i++;
scanf("%f",&a[i]);
}while(a[i]!=-1);
for(int j=0;a[j]!=-1;j++)
{ b[j]=a[j];
n++;}
for(int k=0;k<n-1;k++)
{
m=(b[k]+b[k+1])/2;
printf("%.1f ",m);
}
return 0;
}
Disclaimer: Here you can find all nptel assignment solutions related to CS stream.These may help you for your assignment.The answers are only for verification.You cannot copy these directly from the post(code of conduct of NPTEL).These is not 100% correct solutions.
great work guys.i will be always thankful to you
ReplyDelete