# C Program To Find Average Age

This program finds the average age of a person using an array. The array which stores a list of age is passed on to a function and the function computes the average age and returns a value.

Before you begin with the example, learn basic concepts of C programming language such as arrays, function, and operators. If you know them already, continue reading.

## Problem Definition

To find the average age of person the program does the following tasks.

• Read all the values in an array.
• Pass the entire array to function average( ).
• Function average() computes and returns a value.

First, we store all the values of age in the array during array declaration. Do not mention the size during array declaration because it is automatically determined when array members are assigned.

For example

int arr[] = {20, 34, 45, 26, 58, 33};

The above declaration has 6 members; therefore, the size of the array is 6. Second, the array is passed on the function when you call the function in the main program.

main()
{
avg = average(arr);
}

The value returned by function average will be assigned to variable avg. When the array is passed on to the function, only the reference to the first member of the array is passed. Other members are located using the reference to the first member.

## Program Source Code

#include <stdio.h>
#include <stdlib.h>
int average(int arr[]); /* Function declaration */
int main()
{
int avg;
int arr[] = {20, 34, 45, 26, 58, 33}; /* Array of ages */
avg = average(arr); /* calling function average */
printf("Average Age = %d\n",avg);
system("PAUSE");
return 0;
}

/* Function Definition - Average() */
int average(int a[])
{
int i;
int avg, sum=0; /* local variables */
for(i=0;i<6;i++)
{
sum = sum + a[i];
}
avg = (sum/6);
return avg;
}

## Output

The output of the above program is given below. The average age is 36 because 20 + 34 + 45 + 26 + 58 + 33 = 216/6 = 36.

Therefore, the program works fine.

Average Age = 36
Press any key to continue . . .