Practical 7.6

Posted: January 23, 2012 in Lab 3

Problem statement- Write a function to calculate the (floating point) average of an array of integers.

#include<stdio.h>
float avg(int *, int);
int main()
{
int a[20],i,n;
printf("Enter the no. of elements\n");
scanf("%d",&n);
printf("Entre elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
avg(&a[0],n);
return 0;
}
float avg(int *a, int n)
{
int i;
float avg,s=0;
for(i=0;i<n;i++)
s=s+*(a+i);
avg=s/n;
printf("Avg is %f",avg);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s