Thread: passing 2d arrays as parameters

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    30

    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;
    	
    	if(readmatrix(array,m,n)==0)
    		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 --
    please help me...
    Syra
    Amateur's urge to master C/C++

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if(readmatrix(array,m,n)==0)
    Should be
    if(readmatrix(array,&m,&n)==0)

    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    30
    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;
    	
    	if(readmatrix(array,&m,&n)==0)
    		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..
    please help me...
    Syra
    Amateur's urge to master C/C++

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    30
    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
    Syra
    Amateur's urge to master C/C++

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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)++
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    30
    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.
    please help
    Syra
    Amateur's urge to master C/C++

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    30
    i am still not getting it
    please help me
    Syra
    Amateur's urge to master C/C++

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Be more specific, I just tried it here
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Oct 2004
    Posts
    30
    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
    please explain.
    Syra
    Amateur's urge to master C/C++

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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:
    Quote 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;
    }
    Quote 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.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Oct 2004
    Posts
    30
    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?

    please explain.......
    Syra
    Amateur's urge to master C/C++

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > and i don't get it . why?
    Because at some point, my function gets to return 1, and yours doesn't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing 2 dimensional arrays to functions
    By homeyg in forum C++ Programming
    Replies: 7
    Last Post: 01-09-2005, 03:16 PM
  2. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM
  3. 2d arrays
    By snowy101 in forum C++ Programming
    Replies: 8
    Last Post: 07-08-2002, 04:05 PM
  4. passing arrays
    By Sarah in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2001, 02:50 PM
  5. passing arrays
    By Sarah in forum C Programming
    Replies: 1
    Last Post: 10-24-2001, 08:02 PM