Ask Experts Questions for FREE Help !
Ask
    jbd83's Avatar
    jbd83 Posts: 4, Reputation: 1
    New Member
     
    #1

    Feb 7, 2008, 09:58 AM
    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;
    }
    PolluxCastor's Avatar
    PolluxCastor Posts: 117, Reputation: 5
    Junior Member
     
    #2

    Feb 7, 2008, 07:28 PM
    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?
    jbd83's Avatar
    jbd83 Posts: 4, Reputation: 1
    New Member
     
    #3

    Feb 8, 2008, 03:31 AM
    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
    PolluxCastor's Avatar
    PolluxCastor Posts: 117, Reputation: 5
    Junior Member
     
    #4

    Feb 8, 2008, 03:39 AM
    You can download Visual Studio from here Visual Studio 2008 Express Editions
    PolluxCastor's Avatar
    PolluxCastor Posts: 117, Reputation: 5
    Junior Member
     
    #5

    Feb 8, 2008, 03:42 AM
    Why do you have 2 for loops in transpose?
    jbd83's Avatar
    jbd83 Posts: 4, Reputation: 1
    New Member
     
    #6

    Feb 8, 2008, 03:50 AM
    No good reason, sorry should have taken that out... doesn't make any difference to the result though.

    Jbd
    jbd83's Avatar
    jbd83 Posts: 4, Reputation: 1
    New Member
     
    #7

    Feb 8, 2008, 03:51 AM
    Will try Visual Studio, thanks very much.

    Jbd

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

2weeks late & passing passing clots [ 1 Answers ]

My period is weeks late and yesterday I had some bad cramping. I had some light bleeding & then I passed to large clots. I have never had any clots this size before. I am still bleeding. Is this just a late period or am I having a miscarriage ?

Asians finding work in Europe [ 2 Answers ]

Hi, I'm Richard single and on my mid 20's, I'm presently looking for a job in the European region. I have an engineering diploma and various experience in engineering(semiconductor sector). I would like to ask my chances if I pursue this plan. And if it's not feasible at this time - what are the...

Finding distance using Work [ 5 Answers ]

Suppose a gravel ramp slopes upward at 6.0 degrees and the coefficient of friction is 0.40. Use work and energy to find the length of a ramp that will stop a 15000kg truck that enters the ramp at 35 m/s. This is what I did: I calculated Ff and got 6146.4N. Plugged that into the work formula....

How to get the work done without going for argument [ 4 Answers ]

I'm married recently .My wife is abit sentimental and so one have to be very careful when he/she interacts with her. In short I feel she is yet to be matured. I want her to continue her studies.But the problem is she has lost interest in it .Though she also wants to complete her graduation...

Unsound argument and invalid argument [ 1 Answers ]

What is the difference between an unsound argument and an invalid argument? Give an example of each. When you are building an argument for an issue that is significant to you, do you think it is more important to be sound or valid? Why?


View more questions Search