ADVERTISEMENT
ADVERTISEMENT

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

ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.