
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:
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