Thread: Given a matrix, write a function which... (transposition)

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    33

    Given a matrix, write a function which... (transposition)

    Hi everyone!
    I should resolve this exercise, but I think I have some problems.
    "Given a matrix written as a monodimensional array, write a function which change this matrix into his transposed".

    Note: A Matrix can be written as a multidimensional array ( A[i][j] ) or as a monodimensional array ( A[i+n+j] ). In this case, it is required to use the monodimensional way.

    Here there is what I written:

    Code:
    void trasp (float *A, int m, int n){
    
    /*m is the number of arrows, n is the number of columns*/
    
    
            float *C;
            C= (float*)calloc(m*n, sizeof(float));
    
    
            for(int i=0;i<m;i++){
                for(int j=0;j<n;j++){
    
                    C[j*m+i] =A[i*n+j];
    
    
                }
            }
            C=A;
            free(C);
    }
    I think that there are some mistakes, because it is not working.
    The "multidimensional" way is easier.
    This one should be easy too, but, I don't know why, I'm having some problems.

    Thanks everyone!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Code:
    C=A;
    free(C);
    What do you think these two lines do?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    33
    Quote Originally Posted by GReaper View Post

    What do you think these two lines do?
    After I created the transposed of A (C), I assign C to A.
    Doing this, the matrix given as input will be actually transformed into its transposed.
    After doing that, I delete C, which becomes useless.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    But you don't. Assignment goes from right to left, here you assign the pointer of matrix A to the pointer of matrix C, and then free/delete C(which is now A). If you wanted to change the 'A' matrix you need to take it as a double pointer, like this:
    Code:
    void trasp (float **A, int m, int n){
        float *C;
        
        /* Perform transpose here */
        
        free(*A);
        *A = C;
    }
    or, in my opinion better, return C and let the caller decide what they want to do with it:
    Code:
    float* trasp (float *A, int m, int n){
        float *C;
        
        /* Perform transpose here */
        
        return C;
    }
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jun 2019
    Posts
    33
    Quote Originally Posted by GReaper View Post
    But you don't. Assignment goes from right to left, here you assign the pointer of matrix A to the pointer of matrix C, and then free/delete C(which is now A). If you wanted to change the 'A' matrix you need to take it as a double pointer, like this:

    Firstable, thank you for your advices.

    Unluckly, the code is still not working.
    Here there is the function:

    Code:
    void traspo (float *A, int m, int n){
    
    
            float *C;
            C= (float*)calloc(m*n, sizeof(float));
            for(int i=0;i<m;i++){
                for(int j=0;j<n;j++){
                    C[m*j+i] =A[n*i+j];
    
    
                }
            }
            free(A);
            A=C;
    }
    My matrix to transpose has two arrows and three columns.
    In the first arrow it has -> 0 , 1 , 2
    In the sec. arrow it has-> 0 , 2 , 4

    When I try to transpose it, unluckly, the second arrow of the transposed matrix isn't correct.
    I send you the code where there is written also the creation of the initial matrix, and the printf (the function transpo is the one I written above).

    Code:
    void traspo (float *A, int m, int n);
    
    
    void main(){
        
        float *Q;
        int a=2;
        int c=3;
        
        Q= (float*)calloc (r*c, sizeof(float));
    
    
        int i, j;
    
    
            for (i=0; i<a; i++){
                for (j=0; j<c; j++){
                    
                    Q[i*c+j]=(float)(i+1)*j;
                    printf("the value of the arrow %d and column %d of the matrix Q is %f\n\n", i+1,j+1,Q[i*c+j]);
    
    
    
    
                }
    
    
            }
            
            printf("\n\n\n I TRANSPOSE Q MATRIX \n\n\n");
            traspo(Q, a, c);
            for (i=0; i<c; i++){
                    for (j=0; j<a; j++){
                        printf("the value of the arrow %d and column %d of the matrix Q is %f\n\n", i+1,j+1,Q[i*a+j]);
                    }
            }
    
    
    
    
    }
    The transposed matrix should be like this one:

    first arrow -> 0 , 0
    sec. arrow -> 1 , 2
    thir. arrow -> 2 , 4

    But, instead, it is like this:


    first arrow -> 0 , 0
    sec.arrow -> 2 , 0
    thir. arrow -> 2 , 4

  6. #6
    Registered User
    Join Date
    Jun 2019
    Posts
    33
    Quote Originally Posted by GReaper View Post
    But you don't. Assignment goes from right to left, here you assign the pointer of matrix A to the pointer of matrix C, and then free/delete C(which is now A). If you wanted to change the 'A' matrix you need to take it as a double pointer, like this:

    ALL FINE GReaper, I solved that!

    Thank you for your time!

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    By the way, the main function returns int, not void.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  2. Replies: 1
    Last Post: 09-19-2009, 02:17 PM
  3. I need help for matrix transposition FORMATING
    By masked_blueberr in forum C Programming
    Replies: 6
    Last Post: 07-19-2005, 04:59 PM
  4. Bit Transposition such as D.E.S Initial Permutation
    By silverwatch in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2003, 06:06 AM
  5. Need help to write a program for 3X3 Inverse matrix
    By Ramprasad in forum C Programming
    Replies: 1
    Last Post: 11-26-2001, 08:23 AM

Tags for this Thread