13,139,254 members (75,144 online)
Rate this:
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......

```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.....
```
float trpse(int,int,float[20][20],float[20][20]);
float A[20][20],At[20][20],yb[20][20],Ayb[20][20];
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 14-Oct-12 4:59am
N Shiva531
Updated 14-Oct-12 5:41am
v2
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...

Rate this:

## Solution 1

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!
Rate this:

## Solution 2

```// 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];
}
}
}```

Top Experts
Last 24hrsThis month
 Graeme_Grant 593 OriginalGriff 374 Karthik Bangalore 155 CPallini 155 Dave Kreskowiak 135
 OriginalGriff 4,857 Graeme_Grant 4,783 ppolymorphe 1,679 Jochen Arndt 1,522 CPallini 1,390