C Language Tutorials by Ghulam Murtaza Dahar - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Output

4

Single element of an array can be passed in similar manner as passing variable to a function.

Passing entire one-dimensional array to a function

While passing arrays to the argument, the name of the array is passed as an argument(,i.e, starting address of memory area is passed as argument).

Write a C program to pass an array containing age of person to a function. This function should find average age and

display the average age in main function.

#include <stdio.h>

float average(float a[]);

int main(){

float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18};

avg=average(c); /* Only name of array is passed as argument. */

printf("Average age=%.2f",avg);

return 0;

}

float average(float a[]){

int i;

float avg, sum=0.0;

for(i=0;i<6;++i){

sum+=a[i];

}

avg =(sum/6);

return avg;

}