1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

problem in returning a matrix(2d array) to main function

Discussion in 'C' started by rohanag, Oct 26, 2007.

  1. rohanag

    rohanag New Member

    Joined:
    Oct 21, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Studying
    Location:
    Jaipur
    in my program, i've taken the input from user, passed the 2 input matrices to the addtn function which performs addition,
    but the function is not returning the correct values to main,it is actually returning garbage values
    Mainly, i want to learn how to return a 2d array to the main module

    This is main module
    Code:
    #include<stdio.h>
    #include<conio.h>
    void input(int (*in)[3]);
    int *addtn(int (*x)[3],int (*y)[3]);
    void main()
    {
    int mat1[3][3],mat2[3][3],choice;
    int i,j,(*res)[3];
    clrscr();
    
    printf("\nEnter the elements of first matrix:\n");
    input(mat1);
    printf("\nEnter the elements of second matrix:\n");
    input(mat2);
    
    do
    	{
    	clrscr();
    	printf("\t\t\t 1) ADDITION\n");
    	printf("\t\t\t 2) EXIT\n\n\n");
    	printf("\n\nEnter Your choice: ");
    	scanf("%d",&choice);
    		switch(choice)
    		{
    		case 1:
    			res=addtn(mat1,mat2);
    			for(i=0;i<3;i++)
    			{
    				for(j=0;j<3;j++)
    				{
    				printf("%d\t",*(*(res+i)+j));
    				}
    			}
    			getch();
    			break;
    		case 2:
    			exit(1);
    		default :
    			printf("Not in the list of choices. Enter from the list only!");
    		}
    
    }while(1);
    }
    
    This is for input
    Code:
    //********************************INPUT**************************
    void input(int (*in)[3])
    {
    int i,j;
    for(i=0;i<3;i++)
    {
    	for(j=0;j<3;j++)
    	{
    		printf("\nEnter the Element of row,%d and col,%d: ",i+1,j+1);
    		scanf("%d",(*(in+i)+j));
    	}
    }
    getch();
    clrscr();
    }
    
    For addition, the problem area, which is not returning correctly
    Code:
    int *addtn(int (*x)[3],int (*y)[3])
    {
    int i,j,c[3][3];
    for(i=0;i<3;i++)
    	for(j=0;j<3;j++)
    		c[i][j]=0;
    
    
    for(i=0;i<3;i++)
    {
    	for(j=0;j<3;j++)
    	{
    		c[i][j]= *(*(x+i)+j)+ *(*(y+i)+j);    //performing addition here
    	}
    }
    getch();
    clrscr();
    return(c);             //returning here
    }
    
     
  2. dharmaraj.guru

    dharmaraj.guru New Member

    Joined:
    Oct 23, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Can you please give me the warning/error message if you get?
    If no such, have you tried to print that array?
     

Share This Page