Click here to Skip to main content
15,886,045 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have written a code for finding a transpose of a matrix as a separate program, it worked well.

But when Iam using that function into my main program, Iam getting wrong ans....

This was my code for function......

C#
float trpse(int r,int c,float a[r][c],float atr[c][r])
{   int i,j;
    for(i=0;i<c;i++)
    {
        for(j=0;j<r;j++)
        {
            atr[i][j]=a[j][i];
        }
    }

}



I am using like this.....
C#
float trpse(int,int,float[20][20],float[20][20]);
float A[20][20],At[20][20],yb[20][20],Ayb[20][20];
	printf("for A:\n");  /*Asking for input*/
	for(i=0;i<ar;i++)
	{
		for(j=0;j<ac;j++)
		{
			scanf("%f",&A[i][j]);
		}	
	}
trpse(ar,ac,A,At);
    for(i=0;i<ac;i++)
    {
        for(j=0;j<ar;j++)
        {
            printf("%f ",At[i][j]);
        }
        printf("\n");
    }


For example:
My input is.....

for A:
1 2 3
4 5 6
and
Iam expecting to get this out of the function trpse:
1 4
2 5
3 6

But I am getting this.
1.000000 0.000000
0.000000 0.000000
0.000000 0.000000
Posted
Updated 14-Oct-12 5:41am
v2
Comments
OriginalGriff 14-Oct-12 11:06am    
What are you expecting to get, and what do you get?
Use the "Improve question" widget to edit your question and provide better information.
N Shiva 14-Oct-12 11:42am    
See once.. I edited the question...

OK.
I think I know what the problem is, but without running it I can't be sure.
So, it's up to you to debug it!

Start like this (assuming you don't have a debugger - I don't know what flavour of C you are using)
Make a function that prints a two x two array.
Call it before you call trpse to check your input looks good.
Call it again after you call trpse to check what happens to your output.

If the input looks ok, and the output screwie, move the calls into trpse and check input as the first thing the function does, and output as the last.

Are they the same as the before and after outside the function. I'm betting not!
 
Share this answer
 
Replace your function to:

C++
// forward declaration:
float trpse(int r,int c,float **a,float **atr);

...

// function definition:
float trpse(int r,int c,float **a,float **atr)
{   int i,j;
    for(i=0;i<c;i++)>
    {
        for(j=0;j<r;j++)>
        {
            atr[i][j]=a[j][i];
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900