-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53ea50b
commit 102e1f2
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//Passing 2D array to function | ||
|
||
#include<stdio.h> | ||
|
||
double average(int [][2], int, int); //no need to specify array/variable names in this step | ||
|
||
void main() | ||
{ | ||
int M=3,N=2; | ||
|
||
double mean; | ||
int matrix[3][2]= | ||
{ | ||
{1,2}, | ||
{3,4}, | ||
{5,6} | ||
}; | ||
|
||
mean = average(matrix,M,N); | ||
printf("average over the matrix is %f", mean); | ||
} | ||
|
||
double average(int x[][2],int M,int N) // require bounds for all dimensions except the first | ||
{ | ||
int i, j; | ||
double sum = 0.0; | ||
for (i=0; i<M; i++) | ||
for(j=0; j<N; j++) | ||
sum += x[i][j]; | ||
return(sum/(M*N)); | ||
} |