12,760,014 members (31,168 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];
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 14-Oct-12 5:59am
N Shiva531
Updated 14-Oct-12 6: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

Replace your function to:

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

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

Top Experts
Last 24hrsThis month
 OriginalGriff 230 Peter Leow 70 Karthik Bangalore 60 Graeme_Grant 49 Bryian Tan 45
 OriginalGriff 4,557 Peter Leow 3,319 ppolymorphe 2,723 Karthik Bangalore 2,634 Graeme_Grant 2,426

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 14 Oct 2012