Home » C Program To Find Average Age

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.

      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.

(adsbygoogle = window.adsbygoogle || []).push({}); </script

Please support us by disabling your adblocker or whitelist this site from your adblocker. Thanks!

turn of adblocker imag

(adsbygoogle = window.adsbygoogle || []).push({});