Reputation: 13
I am new to C and I am learning from "Programming in C" by Stephen G. Cochan. I have been given next exercise:
12.A matrix M with i rows, j columns can be transposed into a matrix N having j rows and i columns by simply setting the value of N a,b equal to the value of M b,a for all relevant values of a and b.
a) Write a function transposeMatrix that takes as an argument a 4 x 5 matrix and a 5 x 4 matrix. Have the function transpose the 4 x 5 matrix and store the results in the 5 x 4 matrix. Also write a main routine to test the function.
I have done something wrong with the arguments.
The errors I'm getting are: warning: return makes integer from pointer without a cast [enabled by default]
passing argument 1 of ‘transposeMatrix’ makes pointer from integer without a cast [enabled by default]
expected ‘int (*)[5]’ but argument is of type ‘int’ (It seems to me like this can be ignored)
etc..all about arguments.. I know code is not perfect but i think it should work if array was returned correctly and arguments were fixed..but I can't find a way to fix it..
// Program to transpose M matrix to N matrix
#include <stdio.h>
int transposeMatrix(int matrixM[][5], int matrixN[][4]) {
int i, j;
i = 0;
j = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
matrixN[j][i] = matrixM[i][j];
}
}
return matrixN;
}
int main(void) {
int i, j;
int matrixM[4][5] = {{12, 25, 47, 87, 54},
{16, 89, 78, 63, 58},
{45, 21, 47, 62, 82},
{14, 56, 47, 41, 98}};
int matrixN[5][4];
transposeMatrix(matrixM[4][5], matrixN[5][4]);
i = 0;
j = 0;
for (j = 0; j < 5; j++) {
for (i = 0; i < 4; i++) {
printf("%i ", matrixN[j][i]);
}
printf("\n");
}
return 0;
}
Upvotes: 1
Views: 528
Reputation: 335
Actually the code linked above doesn't really work, it's just printing a transposed matrix by switching rows with columns in the printf() call, it does not truly transpose the matrix as the exercise requires (you can avoid calling transposeMatrix at all and the result is the same). Pay attention to how the exercise is worded, you should use a function and store the results in a new matrix. Also, at this point in the book we're not supposed to use pointers (yet).
This is how I did it:
/*
A matrix M with i rows, j columns can be transposed into a matrix N having j rows and i columns
by simply setting the value of Na,b equal to the value of Mb,a for all relevant values of a and b.
Write a function transposeMatrix() that takes as an argument a 4 × 5 matrix and a 5 × 4
matrix. Have the function transpose the 4 × 5 matrix and store the results in the 5 × 4 matrix. Also
write a main() routine to test the function.
*/
#include <stdio.h>
void transposeMatrix(int matrix45[4][5], int matrix54[5][4])
{
int x, y;
for (x = 0; x < 4; x++)
for (y = 0; y < 5; y++)
matrix54[y][x] = matrix45[x][y];
}
int main(void)
{
int x, y;
int myMatrix[4][5] = { {0, 1, 2, 3, 4},
{5, 6, 7, 8, 9},
{10, 11, 12, 13, 14},
{15, 16, 17, 18, 19} };
int myTransposedMatrix[5][4];
printf("Original Matrix: \n\n");
for (x = 0; x < 4; x++)
{
for (y = 0; y < 5; y++)
{
printf("%3i", myMatrix[x][y]);
}
printf("\n");
}
transposeMatrix(myMatrix, myTransposedMatrix);
printf("\nTransposed Matrix: \n\n");
for (x = 0; x < 5; x++)
{
for (y = 0; y < 4; y++)
{
printf("%3i", myTransposedMatrix[x][y]);
}
printf("\n");
}
return 0;
}
Upvotes: 0
Reputation: 726909
There are two ways a function can pass data back to the caller:
The first way involves copying, and is inefficient for larger values. The second way is preferred when a large value needs to be returned without copying, or when you need to return multiple results.
Another problem is passing the arrays: your call should pass array names without indexes, like this:
transposeMatrix(matrixM,matrixN);
Your code is using the second strategy. However, it does not need to return anything else. Therefore, the proper return type for your function should be void
, not int
. Change the return type, and remove the return statement to fix this issue.
Upvotes: 1