1. ## Transpose Matrix

Im busy trying to transpose my array but the code doesnt seem to be working...Can someone help me out please?

Code:
```#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

#define N 3

int find_inner_product (int *a,int *b,int size)

{int inner_product=0,i=0;
for (i=0;i<size;i++)

{inner_product=inner_product+ *(a+i)*(*(b+i));}

return inner_product;} /*function to do the inner product*/

int matrix_transpose(int a[][N],int size)

{int i=0,j=0,temp[N][N],transpose_matrix[N][N];
for	( i = 0; i<N; i++ )
{for( j = 0; j<N; j++ )

{		a[i][j]=temp[i][j];
temp[i][j]=transpose_matrix[j][i];
}
}
}

int main(void)
{
int matrix1[N][N],matrix2[N][N],i=0,j=0,inner_product=0,transpose_matrix[N][N];

printf("Please enter the values of the 3x3 Matrix: ");

for	( i = 0; i < N; i++ )
{for( j = 0; j< N; j++ )

{scanf("%d", &matrix1[i][j]);}

} /*this for loop goes through every number and stores it in the first matrix*/
printf("\nPlease enter the values of the second 3x3 Matrix: ");

for	( i = 0; i < N; i++ )
{for( j = 0; j< N; j++ )

{scanf("%d", &matrix2[i][j]);}
} /*this for loop goes through every number and stores it in the second matrix*/

inner_product=find_inner_product(matrix1,matrix2,N); /*calling the function to do the first row dot product*/
printf("\nThe inner product between the top two rows is %d",inner_product);

for	( i = 0; i < N; i++ )
{for( j = 0; j< N; j++ )
transpose_matrix[i][j]=matrix_transpose( matrix1[N][N],N);
}

for	( i = 0; i < N; i++ )
{for( j = 0; j< N; j++ )
printf("\n%d", transpose_matrix[i][j]);
}
}```

2. I'll help with some cosmetic changes that make it easier to read:
Code:
```#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

#define N 3

int find_inner_product (int *a, int *b, int size)
{
int inner_product = 0;
int i = 0;
for (i = 0; i < size; i++)
{
inner_product += a[i] * b[i];
}

return inner_product;
}

int matrix_transpose(int a[][N], int size)
{
int temp[N][N], transpose_matrix[N][N];
int i = 0, j = 0;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
a[i][j] = temp[i][j];
temp[i][j] = transpose_matrix[j][i];
}
}
}

int main(void)
{
int matrix1[N][N], matrix2[N][N], transpose_matrix[N][N];
int inner_product = 0;
int i = 0, j = 0;

printf("Please enter the values of the 3x3 Matrix: ");
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
scanf("%d", &matrix1[i][j]);
}
}

printf("\nPlease enter the values of the second 3x3 Matrix: ");
for (i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
{
scanf("%d", &matrix2[i][j]);
}
}

inner_product = find_inner_product(matrix1, matrix2, N); /* first row dot product */
printf("\nThe inner product between the top two rows is %d", inner_product);

for (i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
transpose_matrix[i][j] = matrix_transpose(matrix1[N][N], N);
}

for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
printf("\n%d", transpose_matrix[i][j]);
}
}```

3. The problem is with the transpose function,
Code:
```int matrix_transpose(int a[N][N],int size)

{int i=0,j=0,temp[N][N],transpose_matrix[N][N];
for	( i = 0; i<N; i++ )
{for( j = 0; j<N; j++ )

{		a[i][j]=temp[i][j];
temp[i][j]=transpose_matrix[j][i];
}
}```
Whats the problem?

4. Ive refined it a tiny bit but i still cant seem to find the problem. Again, the transpose doesnt work!

Code:
```#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

#define N 3

int find_inner_product (int *a,int *b,int size)

{int inner_product=0,i=0;
for (i=0;i<size;i++)

{inner_product=inner_product+ *(a+i)*(*(b+i));}

return inner_product;} /*function to do the inner product*/

int matrix_transpose(int a[N][N],int size)

{int i=0,j=0,temp[N][N],transpose_matrix[N][N];
for	( i = 0; i<N; i++ )
{for( j = 0; j<N; j++ )

{		a[i][j]=temp[i][j];
temp[i][j]=transpose_matrix[j][i];
}
}
}

int main(void)
{
int matrix1[N][N],matrix2[N][N],i=0,j=0,inner_product=0,transpose_matrix[N][N];

printf("Please enter the values of the 3x3 Matrix: ");

for	( i = 0; i < N; i++ )
{for( j = 0; j< N; j++ )

{scanf("%d", &matrix1[i][j]);}

} /*this for loop goes through every number and stores it in the first matrix*/
printf("\nPlease enter the values of the second 3x3 Matrix: ");

for	( i = 0; i < N; i++ )
{for( j = 0; j< N; j++ )

{scanf("%d", &matrix2[i][j]);}
} /*this for loop goes through every number and stores it in the second matrix*/

inner_product=find_inner_product(matrix1,matrix2,N); /*calling the function to do the first row dot product*/
printf("\nThe inner product between the top two rows is %d",inner_product);

matrix_transpose(matrix2,N);```

5. Code:
```int matrix_transpose(int a[N][N],int size)

{int i=0,j=0,temp[N][N],transpose_matrix[N][N];
for	( i = 0; i<N; i++ )
{for( j = 0; j<N; j++ )

{		a[i][j]=temp[i][j];
temp[i][j]=transpose_matrix[j][i];
}
}
}```
1) You need to pass in the array as a pointer, not as an array definition.
2) using a[N] does not give you access to N inside the function.
3) transposing (swapping) values requrires 3 steps not 2... save first value, move second value to first, copy saved value to second.
4) When calling the function you correctly passed in the size, but totally ignored it within the function.
5) your temp variable does not need to be an array
6) the function should not call itself... let the loops do their work.

6. Originally Posted by CommonTater
1) You need to pass in the array as a pointer, not as an array definition.
What the OP has in this respect is fine.

6) the function should not call itself... let the loops do their work.
It doesn't. You've confused the name of the function with one of its variables.

2) using a[N] does not give you access to N inside the function.
N is a #define. Again, what the OP has there is fine.

3) transposing (swapping) values requrires 3 steps not 2... save first value, move second value to first, copy saved value to second.
5) your temp variable does not need to be an array
This is the real problem, or at least hints at it (the swap is done incorrectly). In fact, everything is being set to the random uninitialized values in temp[][] and transpose_matrix.[][]:

Code:
```	int i=0,j=0, temp[N][N], transpose_matrix[N][N];
for	( i = 0; i<N; i++ ) {
for( j = 0; j<N; j++ ) {
a[i][j]=temp[i][j];
temp[i][j]=transpose_matrix[j][i];
}
}```
After that, all arrays will be garbage. If Mickie-Zee can tell us what the output is compared to the input, that will help confirm the nature of the problem.

7. What should my three lines of code look like to save tranpose it? The three steps?
All i have at the moment is:
Code:
```int matrix_transpose(int a[N][N],int size)

{int i=0,j=0,temp=0,transpose_matrix[N][N];
for	( i = 0; i<N; i++ )
{for( j = 0; j<N; j++ )

{	temp=a[i][j];
temp=transpose_matrix[i][j];
}
}
}```

8. So you've got a red cup full of water, a blue cup full of tea, and an empty green cup. How would you get the red cup full of tea and the blue cup full of water?

Also: you are currently transposing your matrix twice (once for (i,j) and once for (j,i)), hence when you do get your swap working you won't get the answer you want, since if you swap everything twice it ends up exactly where it was at the beginning. Once you have put the first row into the first column, then you shouldn't mess with the first column when you do the next row.

9. Originally Posted by Micki-Zee
What should my three lines of code look like to save tranpose it? The three steps?
Code:
```temp = a;        // save first value
a = b;          // copy transposed value
b = temp;       // copy saved value```

10. Originally Posted by Micki-Zee
Again, the transpose doesnt work!
You're so far very poor at describing the problem. If you were booking your car in at a garage over the phone and they asked what you wanted them to look at would you simply say that it "doesn't work"?!

You should always tell us what actually happens. "doesn't work" prety much covers ever possible thing that could be wrong. E.g. won't compile, wont run, crashes, does not call the function, does not change the data, turns the data into garbage, produces the wrong answer etc etc, the list goes on... I think I've made my point.

I hope to never near those two words together from you again.

11. Originally Posted by Micki-Zee
The problem is with the transpose function,
...
Whats the problem?
You tell me. You're the one trying to get it to work. What's wrong with it? Ask Questions The Smart Way!

Quzah.