“HAPPINESS SHOULD BE A FUNCTION WITHOUT ANY PARAMETERS”

NPTEL | Week 0: Assignment 3 Question 2 Introduction to Programming in C (14 Sep- 06 Nov) 2020.



        Introduction to Programming in C       



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;
  
}
  
WEEK-0 :INTRODUCTION TO PROGRAMMING IN C

CLICK ON BELOW LINK.
<<CLICK HERE FOR  WEEK 0:ASSIGNMENT 3 QUESTION 3>>

PLEASE SUBSCRIBE OUR CHANNEL.
<CLICK HERE>
FOLLOW OUR WEBSITE FROM THE BUTTON PROVIDED TO THE BOTTOM OF PAGE TO GET SOON ANSWERS OF PROGRAMS.
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.

1 comment:

If you have any problems related to solutions or any concept please let me know.