finding matrix transpose - why doesn't it work when passing pointer argument?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2008
Posts: 2
Reputation: jbd is an unknown quantity at this point 
Solved Threads: 0
jbd jbd is offline Offline
Newbie Poster

finding matrix transpose - why doesn't it work when passing pointer argument?

 
0
  #1
Feb 7th, 2008
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 and still can't work out why. Can anyone help??
Thanks
jbd
  1. int find_transpose(int n, double **a, double **b)
  2. {
  3. int i,j;
  4. for (i=0; i<n; i++)
  5. {
  6. for (j=0; j<n; j++)
  7. b[i][j] = a[i][j];
  8. }
  9. for (i=0; i<n; i++)
  10. {
  11. for (j=0; j<n; j++)
  12. b[i][j] = a[j][i];
  13. }
  14.  
  15. }
  16.  
  17.  
  18. int main (void)
  19. {
  20.  
  21. int i, j, p=3;
  22. double **in, **out;
  23.  
  24. in = malloc(p * sizeof(int *));
  25. out = malloc(p * sizeof(int *));
  26.  
  27. for (i=0; i<p; i++){
  28. in[i]= malloc(p * sizeof(int *));
  29. out[i]= malloc(p * sizeof(int *));}
  30.  
  31. for (i=0; i<p; i++)
  32. {
  33. for (j=0; j<p; j++)
  34. {in[i][j]= 10./(i+1);
  35. printf("in[%i][%i] = %f\n", i, j, in[i][j]);
  36. }
  37. }
  38.  
  39. find_transpose(p, in, out);
  40.  
  41. for (i=0; i<p; i++)
  42. {
  43. for (j=0; j<p; j++)
  44. printf("in[%i][%i] = %f\n", i, j, in[i][j]);
  45. }
  46.  
  47. for (i=0; i<p; i++)
  48. {
  49. for (j=0; j<p; j++)
  50. printf("out[%i][%i] = %f\n", i, j, out[i][j]);
  51. }
  52.  
  53. return
Last edited by Narue; Feb 7th, 2008 at 12:28 pm. Reason: Added code tags, do it yourself next time, please.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: finding matrix transpose - why doesn't it work when passing pointer argument?

 
0
  #2
Feb 7th, 2008
What does not work? Does it gives you compile errors? Does it crashes?
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: finding matrix transpose - why doesn't it work when passing pointer argument?

 
0
  #3
Feb 7th, 2008
I corrected minor syntactical errors in your code. I did not check the logic. Check following code.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int find_transpose(int n, double **a, double **b)
  5. {
  6. int i,j;
  7. for (i=0; i<n; i++)
  8. {
  9. for (j=0; j<n; j++)
  10. b[i][j] = a[i][j];
  11. }
  12. for (i=0; i<n; i++)
  13. {
  14. for (j=0; j<n; j++)
  15. b[i][j] = a[j][i];
  16. }
  17. return 0;
  18. }
  19.  
  20.  
  21. int main (void)
  22. {
  23.  
  24. int i, j, p=3;
  25. double **in, **out;
  26.  
  27. in = (double **)malloc(p * sizeof(int *));
  28. out = (double **)malloc(p * sizeof(int *));
  29.  
  30. for (i=0; i<p; i++){
  31. in[i]= (double *)malloc(p * sizeof(int *));
  32. out[i]= (double *)malloc(p * sizeof(int *));}
  33.  
  34. for (i=0; i<p; i++)
  35. {
  36. for (j=0; j<p; j++)
  37. {in[i][j]= 10./(i+1);
  38. printf("in[%i][%i] = %f\n", i, j, in[i][j]);
  39. }
  40. }
  41.  
  42. find_transpose(p, in, out);
  43.  
  44. for (i=0; i<p; i++)
  45. {
  46. for (j=0; j<p; j++)
  47. printf("in[%i][%i] = %f\n", i, j, in[i][j]);
  48. }
  49.  
  50. for (i=0; i<p; i++)
  51. {
  52. for (j=0; j<p; j++)
  53. printf("out[%i][%i] = %f\n", i, j, out[i][j]);
  54. }
  55.  
  56. return 0;
  57. }
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 2
Reputation: jbd is an unknown quantity at this point 
Solved Threads: 0
jbd jbd is offline Offline
Newbie Poster

Re: finding matrix transpose - why doesn't it work when passing pointer argument?

 
0
  #4
Feb 7th, 2008
Still getting 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!?

Thanks
jbd
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: finding matrix transpose - why doesn't it work when passing pointer argument?

 
0
  #5
Feb 7th, 2008
Watch your types.

Going off of dubydapreek's revision:
  • Lines 7..11: Useless. Get rid of them. (You don't need to copy b to a before you copy b to a.)
  • Lines 27..28: You should be allocating an array of (double *).
  • Lines 31..32: You should be allocating an array of (double).

As a rule, when using malloc(), it should look something like:
type *foo = (type *)malloc( n *sizeof( type ) );
where "type" is whatever type you need it to be. In your case, "type" is first (double*) then it is (double).

Hope this helps.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC