# Thread: passing 2d arrays as parameters

1. ## passing 2d arrays as parameters

not able to understand where the problem is ..
Determining if a mtrix has in it's diagonal - all zeros
Code:
```#include<stdio.h>

int isdiagzero(int a[][10],int m,int n,int *i,int *j)
{
if(((*i)<m)&&((*j)<n))
{
if(a[*i][*j]!=0)
return 0;
else
{
isdiagzero(a,m,n,i++,j++);
}
}
return 1;
}

int readmatrix(int a[][10],int m ,int n)
{
int i,j;
printf("Enter the row and column ");
scanf("%d%d",&m,&n);
if((m==0)||(n==0))
return 0;
else
{
printf("\nEnter the matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
}

void printmatrix(int a[][10],int m ,int n)
{
int i,j;
printf("the matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d",a[i][j]);
printf("\n");
}
}

int main()
{
int array[10][10];
int m,n;

printf("ERROR\n");
else
{
printmatrix(array,m,n);
if(isdiagzero(array,m,n,0,0)==0)
printf("Diagonal not zero !\n");
else
printf("Diagonal is zero !\n");
}
}```
this program reads input as
Enter the row and column 3 3
0 1 1
1 0 1
1 1 0
the matrix
----here it gives exception----
i am not getting it --

Should be

> int readmatrix(int a[][10],int m ,int n)
int readmatrix(int a[][10],int *m ,int *n)

> scanf("%d%d",&m,&n);
scanf("%d%d",m,n);

> for(i=0;i<m;i++)
for(i=0;i<*m;i++)
ditto for the other loop as well.

You need to return the size of the matrix as well, so you need to pass pointers to where the size will be stored.

3. i tried that but the exception is still coming
the output is correct if i use type-iteration
and an exception is generated from the type-recursive

the modified code is
Code:
```#include<stdio.h>
int is = 0;
int isdiagzero(int a[][10],int m,int n,int *i,int *j)
{
if(((*i)<m)&&((*j)<n))
{
if(a[*i][*j]!=0)
return 0;
else
{
is = isdiagzero(a,m,n,i++,j++);		//is this parameter passing right i++,j++
}
}
return is;
}

int readmatrix(int a[][10],int *m ,int *n)
{
int i,j;
printf("Enter the row and column ");
scanf("%d%d",m,n);
if((*m==0)||(*n==0))
return 0;
else
{
printf("\nEnter the matrix\n");
for(i=0;i<*m;i++)
for(j=0;j<*n;j++)
scanf("%d",&a[i][j]);
}
return 1;
}

void printmatrix(int a[][10],int m ,int n)
{
int i,j;
printf("the matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}

int isdiagz(int a[][10],int m ,int n)
{
int i,j;
printf("inside isdiag\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if((i==j)&&(a[i][j]!=0))
return 0;
return 1;
}

int main()
{
int array[10][10];
int m,n;

printf("ERROR\n");
else
{
printmatrix(array,m,n);
if(isdiagzero(array,m,n,0,0)==0)	//type - recursion
//		if(isdiagz(array,m,n)==0)			//type - iteration
printf("Diagonal not zero !\n");
else
printf("Diagonal is zero !\n");
}
getch();
return 0;
}```
this program reads input as
Enter the row and column 3 3
0 1 1
1 0 1
1 1 0
the matrix
0 1 1
1 0 1
1 1 0
----here it gives exception----
i am not getting it --
i suppose the problem lies in understanding recursion..

4. > if(isdiagzero(array,m,n,0,0)==0)
But the last 2 parameters are pointers, so these are NULL pointers
No joy when you dereference them

Code:
```int isdiagzero(int a[][10], int m, int n, int i, int j)
{
if ((i < m) && (j < n)) {
if (a[i][j] != 0)
return 0;
else {
return isdiagzero(a, m, n, i+1, j+1);
}
}
return 0;
}```

5. i had understood that before
and tried writing the code as

Code:
```	    int i=0,j=0;
if(isdiagzero(array,m,n,&i,&j)==0)	//type - recursion
//		if(isdiagz(array,m,n)==0)			//type - iteration
printf("Diagonal not zero !\n");
else
printf("Diagonal is zero !\n");```
no use at all
the out put is the same
please tell me whats wrong

6. Because unless you changed the i++ etc, you incremented the pointer, not what it was pointing at
To make the i you point at increment, you need (*i)++

7. again salem.........

1. this thought too had crossed my mind

Code:
`   	    is = isdiagzero(a,m,n,(*i)++,(*j)++);		//is this parameter passing right i++,j++`
it used to give me some compliation error like
"makes pointer from integer without a cast"
hence i felt that it was wrong assignment

2. let me tell you i tried this too
Code:
`   	    is = isdiagzero(a,m,n,&((*i)++),&((*j)++));`
which gives an error invalid value in unary'&'

3. also tried
Code:
```   	    ti = (*i)++;
tj = (*j)++;
is = isdiagzero(a,m,n,&ti,&tj);```
but the exception comes out

hence what else can it be?
i am not getting it.

8. Code:
```#include<stdio.h>

int isdiagzero(int a[][10], int m, int n, int *i, int *j)
{
if (((*i) < m) && ((*j) < n)) {
if (a[*i][*j] != 0)
return 0;
else {
(*i)++;
(*j)++;
return isdiagzero(a, m, n, i, j);
}
}
return 1;
}

int readmatrix(int a[][10], int *m, int *n)
{
int i, j;
printf("Enter the row and column ");
scanf("%d%d", m, n);
if ((*m == 0) || (*n == 0))
return 0;
else {
printf("\nEnter the matrix\n");
for (i = 0; i < *m; i++)
for (j = 0; j < *n; j++)
scanf("%d", &a[i][j]);
}
return 1;
}

void printmatrix(int a[][10], int m, int n)
{
int i, j;
printf("the matrix\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++)
printf("%d ", a[i][j]);
printf("\n");
}
}

int isdiagz(int a[][10], int m, int n)
{
int i, j;
printf("inside isdiag\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
if ((i == j) && (a[i][j] != 0))
return 0;
return 1;
}

int main()
{
int array[10][10];
int m, n;
int i = 0, j = 0;

if (readmatrix(array, &m, &n) == 0)
printf("ERROR\n");
else {
printmatrix(array, m, n);
if (isdiagzero(array, m, n, &i, &j) == 0) //type - recursion
//      if(isdiagz(array,m,n)==0)           //type - iteration
printf("Diagonal not zero !\n");
else
printf("Diagonal is zero !\n");
}
return 0;
}```

9. i am still not getting it

10. Be more specific, I just tried it here

11. i tried the same code as such but still using the variable 'is'
Code:
```int is = 0;
int isdiagzero(int a[][10],int m,int n,int *i,int *j)
{
if(((*i)<m)&&((*j)<n))
{
if(a[*i][*j]!=0)
return 0;
else
{
(*i)++;
(*j)++;
is = isdiagzero(a,m,n,i,j);		//is this parameter passing right i++,j++
}
}
return is;
}```
now here it gives wrong output
i.e. for
0 1 1
1 0 1
1 1 0
the matrix
0 1 1
1 0 1
1 1 0
diagonal not zero !

does using variable 'is' cause a problem...
(since that is the only diifference in your code and mine)
if yes then what problem does it cause

12. Originally Posted by aldajlo
(since that is the only diifference in your code and mine)
It's ok if you're stupid, but don't lie to us, because we are not:
Originally Posted by Salem
Code:
```int isdiagzero(int a[][10], int m, int n, int *i, int *j)
{
if (((*i) < m) && ((*j) < n)) {
if (a[*i][*j] != 0)
return 0;
else {
(*i)++;
(*j)++;
return isdiagzero(a, m, n, i, j);
}
}
return 1;
}```
Originally Posted by aldajlo
Code:
```int is = 0;
int isdiagzero(int a[][10],int m,int n,int *i,int *j)
{
if(((*i)<m)&&((*j)<n))
{
if(a[*i][*j]!=0)
return 0;
else
{
(*i)++;
(*j)++;
is = isdiagzero(a,m,n,i,j);		//is this parameter passing right i++,j++
}
}
return is;
}```
They are huh? The only thing different is a variable name? You want to try that again?

Quzah.

13. i didn't get it......
what did u mean Quzah.....

it's just using that variable thats making the output go wrong
and i don't get it . why?