| | |
finding matrix transpose - why doesn't it work when passing pointer argument?
![]() |
•
•
Join Date: Feb 2008
Posts: 2
Reputation:
Solved Threads: 0
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
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
c Syntax (Toggle Plain Text)
int 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
Last edited by Narue; Feb 7th, 2008 at 12:28 pm. Reason: Added code tags, do it yourself next time, please.
Re: finding matrix transpose - why doesn't it work when passing pointer argument?
0
#2 Feb 7th, 2008
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.
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<stdlib.h> int 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]; } return 0; } int main (void) { int i, j, p=3; double **in, **out; in = (double **)malloc(p * sizeof(int *)); out = (double **)malloc(p * sizeof(int *)); for (i=0; i<p; i++){ in[i]= (double *)malloc(p * sizeof(int *)); out[i]= (double *)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; }
I know I am. Therefore I am.
•
•
Join Date: Feb 2008
Posts: 2
Reputation:
Solved Threads: 0
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
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
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:
As a rule, when using malloc(), it should look something like:
where "type" is whatever type you need it to be. In your case, "type" is first (double*) then it is (double).
Hope this helps.
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.
![]() |
Other Threads in the C Forum
- Previous Thread: Please dont email to Mr. szalwinski I apologize for my mistake
- Next Thread: how we call another .c file in a .c file??
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






