Ask Experts Questions for FREE Help!
  Advanced
Register  |  Log in  
   Ask    
 Answer  
  Help  

Ask QuestionsprogressAnswer QuestionsprogressBuild ReputationprogressBecome an Expert
 
Free Answers in 3 Easy Steps

Register Now
3 Steps

At Ask Me Help Desk you can ask questions in any topic and have them answered for free by our experts. To ask questions or participate in answering them you must register for a free account. By registering you will be able to:
  • Get free answers from experts in any of our 300+ topics.
  • Accept money for answers that you provide.
  • Communicate privately with other members (PM).
  • See fewer ads.

Home > Computers & Technology > Programming > Compiled Languages > C   »   finding matrix transpose - why doesn't it work when passing pointer argument?

 
Question Tools Search this Question Display Modes
Question
 
 
#1  
Old Feb 7, 2008, 07:58 AM
jbd83
New Member
jbd83 is offline
 
Join Date: Feb 2008
Posts: 4
jbd83 See this member's comment history on his/her Profile page.
finding matrix transpose - why doesn't it work when passing pointer argument?

Hi

I'm adapting some code I've written using 2d arrays (to represent matrices) to handle large arrays such that double matrix[][] goes to double **matrix and then I'm using malloc.

It seems to work fine for part of my program up to where I have to find the matrix transpose at which point it does something I don't understand. I've taken that bit of code out and run it by itself (included below), get the same problem... which is that the input matrix is getting modified when the function is called, here's an example for a test 3 by 3:
The input is:in[0][0] = 10.000000
in[0][1] = 10.000000
in[0][2] = 10.000000
in[1][0] = 5.000000
in[1][1] = 5.000000
in[1][2] = 5.000000
in[2][0] = 3.333333
in[2][1] = 3.333333
in[2][2] = 3.333333

but after being passed to find_transpose is comes out as:in[0][0] = 10.000000
in[0][1] = 10.000000
in[0][2] = 10.000000
in[1][0] = 10.000000
in[1][1] = 5.000000
in[1][2] = 10.000000
in[2][0] = 3.333333
in[2][1] = 3.333333
in[2][2] = 10.000000

and the actual transpose output is:
out[0][0] = 10.000000
out[0][1] = 10.000000
out[0][2] = 10.000000
out[1][0] = 10.000000
out[1][1] = 5.000000
out[1][2] = 3.333333
out[2][0] = 10.000000
out[2][1] = 10.000000
out[2][2] = 10.000000

I really don't understand why!?
Can anyone help??

Thanks
jbd


nt find_transpose(int n, double **a, double **b)
{
int i,j;
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
b[i][j] = a[i][j];
}
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
b[i][j] = a[j][i];
}

}


int main (void)
{

int i, j, p=3;
double **in, **out;

in = malloc(p * sizeof(int *));
out = malloc(p * sizeof(int *));

for (i=0; i<p; i++){
in[i]= malloc(p * sizeof(int *));
out[i]= malloc(p * sizeof(int *));}

for (i=0; i<p; i++)
{
for (j=0; j<p; j++)
{in[i][j]= 10./(i+1);
printf("in[%i][%i] = %f\n", i, j, in[i][j]);
}
}

find_transpose(p, in, out);

for (i=0; i<p; i++)
{
for (j=0; j<p; j++)
printf("in[%i][%i] = %f\n", i, j, in[i][j]);
}

for (i=0; i<p; i++)
{
for (j=0; j<p; j++)
printf("out[%i][%i] = %f\n", i, j, out[i][j]);
}

return 0;
}

Reply With Quote
 
     

Answers
 
 
Old Feb 7, 2008, 05:28 PM   #2  
PolluxCastor
Junior Member
PolluxCastor is offline
 
Join Date: Jan 2008
Posts: 116
PolluxCastor See this member's comment history on his/her Profile page.
This is what I got running your program in Visual Studio 2005

in[0][0] = 10.000000
in[0][1] = 10.000000
in[0][2] = 10.000000
in[1][0] = 5.000000
in[1][1] = 5.000000
in[1][2] = 5.000000
in[2][0] = 3.333333
in[2][1] = 3.333333
in[2][2] = 3.333333
in[0][0] = 10.000000
in[0][1] = 10.000000
in[0][2] = 10.000000
in[1][0] = 5.000000
in[1][1] = 5.000000
in[1][2] = 5.000000
in[2][0] = 3.333333
in[2][1] = 3.333333
in[2][2] = 3.333333
out[0][0] = 10.000000
out[0][1] = 5.000000
out[0][2] = 3.333333
out[1][0] = 10.000000
out[1][1] = 5.000000
out[1][2] = 3.333333
out[2][0] = 10.000000
out[2][1] = 5.000000
out[2][2] = 3.333333


I changed find_transpose() to a void function, and I casted your malloc s

Other than that I didn't do anything.

What compiler are you using?
  Reply With Quote
 
     
 
 
Old Feb 8, 2008, 01:31 AM   #3  
jbd83
New Member
jbd83 is offline
 
Join Date: Feb 2008
Posts: 4
jbd83 See this member's comment history on his/her Profile page.
Hi

I've tried compiling with gcc version 4.0.2 20051125 (Red Hat 4.0.2-8)
and gcc version 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)
... both with the same result

Thanks
jbd
  Reply With Quote
 
     
 
 
Old Feb 8, 2008, 01:39 AM   #4  
PolluxCastor
Junior Member
PolluxCastor is offline
 
Join Date: Jan 2008
Posts: 116
PolluxCastor See this member's comment history on his/her Profile page.
You can download Visual Studio from here Visual Studio 2008 Express Editions
  Reply With Quote
 
     
 
 
Old Feb 8, 2008, 01:42 AM   #5  
PolluxCastor
Junior Member
PolluxCastor is offline
 
Join Date: Jan 2008
Posts: 116
PolluxCastor See this member's comment history on his/her Profile page.
Why do you have 2 for loops in transpose?
  Reply With Quote
 
     
 
 
Old Feb 8, 2008, 01:50 AM   #6  
jbd83
New Member
jbd83 is offline
 
Join Date: Feb 2008
Posts: 4
jbd83 See this member's comment history on his/her Profile page.
No good reason, sorry should have taken that out... doesn't make any difference to the result though.

jbd
  Reply With Quote
 
     
 
 
Old Feb 8, 2008, 01:51 AM   #7  
jbd83
New Member
jbd83 is offline
 
Join Date: Feb 2008
Posts: 4
jbd83 See this member's comment history on his/her Profile page.
Will try Visual Studio, thanks very much.

jbd
  Reply With Quote
 
     


Question Tools Search this Question
Search this Question:

Advanced Search
Display Modes

 
Similar Sponsors

Similar Questions
Question Asker Topic Answers Last Post
2weeks late & passing passing clots rrr05 Women's Health 0 Jan 21, 2008 03:46 AM
Asians finding work in Europe richard clemente Job Hunting 2 Jan 5, 2008 11:53 PM
Finding distance using Work dks2114 Math & Sciences 5 Oct 9, 2007 12:21 PM
How to get the work done without going for argument frnz Other Family & People 4 May 17, 2007 11:45 PM
unsound argument and invalid argument Tanyak24 Other Subjects 1 May 8, 2007 06:05 AM




Copyright ©2003 - 2007, Ask Me Help Desk.
All times are GMT -8. The time now is 03:02 AM.

Content Relevant URLs by vBSEO 3.0.0 RC6 © 2006, Crawlability, Inc.