Wednesday, 2 August 2017

Passing Array to function


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

Solution :


#include<stdio.h>
#include<conio.h>
int avg(int[],int);
void main()
{
     int ar[5],i;
     clrscr();
     for(i=0;i<5;i++)
     {
          printf("Enter %d number
>> ",i+1);
          scanf("%d",&ar[i]);
     }
     printf("\n\tAverage = %d",avg(ar,5));
     getch();
}
int avg(int ar[],int s)
{
     int i=0,sum=0;
     for(i=0;i<s;i++)
          sum+=ar[i];
     return(sum/s);;

}

Download Source Code