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

1. ## 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. Code:
```C=A;
free(C);```
What do you think these two lines do? 3. Originally Posted by GReaper 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. 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;
}``` 5. Originally Posted by GReaper 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:

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. Originally Posted by GReaper 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!  Popular pages Recent additions array, matrix, monodimensional, number, written 