Tuesday, 16 January 2018

Array Average using function

Write a function which accepts an array of size n containing integer values and returns the average of all values. Call the function from main program.

Solution : 

/*==============================
     Girfa Student Help
     Program : Array Average using function
     More Program :http://girfahelp.blogspot.in/p/function-programming.html
================================*/
#include<stdio.h>
#include<conio.h>
#define MAX 5
int avg(int*);

void main()
{
     int ar[MAX],i;
     clrscr();
     for(i=0;i<MAX;i++)
     {
          printf("Enter %d number>> ",i+1);
          scanf("%d",&ar[i]);
     }
     printf("\n\nAverage is %d",avg(ar));
     getch();
}
int avg(int *a)
{
     int i,s;
     for(i=0,s=0;i<MAX;i++,a++)
          s+=*a;
     return(s/MAX);
}

1 comment: