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.

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 Codes

#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;
}
#include <iostream>
using namespace std;

/* Function declaration */
int average(int arr[]);

int main() {
    int avg;
    int arr[] = {20, 34, 45, 26, 58, 33};   /* Array of ages */

    avg = average(arr);   /* calling function */
    cout << "Average Age = " << avg << endl;

    return 0;
}

/* Function Definition - average() */
int average(int a[]) {
    int sum = 0;

    for (int i = 0; i < 6; i++) {
        sum += a[i];
    }

    return sum / 6;
}
public class AverageAge {

    /* Function to calculate average */
    static int average(int[] a) {
        int sum = 0;

        for (int i = 0; i < 6; i++) {
            sum += a[i];
        }

        return sum / 6;
    }

    public static void main(String[] args) {
        int avg;
        int[] arr = {20, 34, 45, 26, 58, 33};   /* Array of ages */

        avg = average(arr);   /* calling function */
        System.out.println("Average Age = " + avg);
    }
}
# Function to calculate average
def average(a):
    sum = 0
    for i in range(6):
        sum += a[i]
    return sum // 6

# Main program
arr = [20, 34, 45, 26, 58, 33]   # Array of ages
avg = average(arr)

print("Average Age =", avg)

Output

The output of the above program is given below. The average age is 36 because:

\begin{aligned} &20 + 34 + 45 + 26 + 58 + 33 \\
&= \frac{216}{6}\\
&= 36
\end{aligned}

Therefore, the program works fine.

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