Skip to content

Commit

Permalink
added array to func
Browse files Browse the repository at this point in the history
  • Loading branch information
adithyaanilkumar committed Jun 29, 2020
1 parent 53ea50b commit 102e1f2
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions C/2d_array_to_func.c
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));
}

0 comments on commit 102e1f2

Please sign in to comment.