C - Passing Two Dimensional (Two D) Array using C Program.
IncludeHelp
02 August 2016
In this code snippet, we will learn how to pass two dimensional array in a user define function. In this example we will pass a two dimensional array in user define function and function will return average of all two dimensional array elements.
C Code Snippet - Pass Two Dimensional Array in User Define Function
//C – Passing Two Dimensional (Two D) Array using C Program.
#include <stdio.h>
//function to pass two d array in function
//this function will return average of all elements
//Author : www.includehelp.com
float averageOfTwoDElements(int x[][3],int row,int col){
int i,j;
int sum=0,totElements;
totElements=row*col;
for(i=0;i<row;i++){
for(j=0;j<col;j++){
sum+=x[i][j];
}
}
return ((float)sum/(float)totElements);
}
int main(){
int arr[3][3]={{1,2,3},{4,5,6},{7,8,9}};
float avg;
//Average of all elements
avg=averageOfTwoDElements(arr,3,3);
printf("Average is: %f\n",avg);
return 0;
}
Average is: 5.000000