943,548 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1079
  • C RSS
Feb 7th, 2008
0

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

Expand Post »
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.
jbd
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jbd is offline Offline
4 posts
since Feb 2008
Feb 7th, 2008
0

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

What does not work? Does it gives you compile errors? Does it crashes?
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Feb 7th, 2008
0

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

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. }
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Feb 7th, 2008
0

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

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
jbd
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jbd is offline Offline
4 posts
since Feb 2008
Feb 7th, 2008
0

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

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Please dont email to Mr. szalwinski I apologize for my mistake
Next Thread in C Forum Timeline: how we call another .c file in a .c file??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC