Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Beginner maths arrays
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 Shiva526
Edited 14-Oct-12 6:41am
v2
Comments
OriginalGriff at 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 at 14-Oct-12 11:42am
   
See once.. I edited the question...
Rate this: bad
good
Please Sign up or sign in to vote.

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!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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];
        }
    }
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,045
1 DamithSL 4,611
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 14 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100