I made this program to get the matrix a
and matrix b
that is a transposition matrix of matrix a
.
The problem is that I couldn't get an accurate transposition matrix.
I checked the functions several times, but I couldn't find which part was wrong.
And there were no errors when compiling.
#include <stdio.h>
typedef struct
{
int row;
int col;
int value;
} term;
I declared the structure term that I use throughout my program.
void transpose(term a[], term b[])
{
int n, currentb;
n = a[0].value; // Total number of elements
b[0].row = a[0].col; // Number of rows in b = Number of columns in a
b[0].col = a[0].row; // Number of columns in b = Number of rows in a
b[0].value = n;
if (n > 0) // A nonzero matrix
{
currentb = 1;
for (int i = 0; i < a[0].col; i++) // Starting from the 0th column
{
for (int j = 1; j <= n; j++) // Find the element from the current column
{
if (a[j].col == i)
{
// Add the elements in the current column to b.
b[currentb].row = a[j].col;
b[currentb].col = a[j].row;
b[currentb].value = a[j].value;
currentb++;
}
}
}
}
}
At first, I thought the function above was wrong.
So, I have looked at the logic several times and I couldn't find the wrong part.
int main()
{
term a[3] = { {1,2,3}, {4,5,6}, {7,8,9} }
term b[3];
// print term a
printf("original matrix\n");
for (int i = 0; i < 3; i++)
{
printf(" %d %d %d\n", a[i].row, a[i].col, a[i].value);
}
printf("\n\n");
// call the transpose function
void transpose(term a[], term b[]);
// print term b
printf("transpose matrix\n");
for (int i = 0; i < 3; i++)
{
printf(" %d %d %d\n", b[i].row, b[i].col, b[i].value);
}
return 0;
}
Next, I reviewed the line that calls the transpose function.
Because the output result was like this:
original matrix
1 2 3
4 5 6
7 8 9
transpose matrix
-858993460 -858993460 -858993460
-858993460 -858993460 -858993460
-858993460 -858993460 -858993460
I have already searched more than 10 pages of questions, but I haven't found a question that has the same wrong result like mine that uses a structure to get the transposition matrix.
And if there was no problem with the code, the result should have been as follows.
original matrix
1 2 3
4 5 6
7 8 9
transpose matrix
1 4 7
2 5 8
3 6 9
Please check my code, i would appreciate it if you could tell me if there is anything wrong.
The way you represent matrices is inappropriate for the assignment: you use arrays of term
structures, each with a row
, col
and value
fields. This representation is valid for sparse matrices, and transposing this representation is easy: just swap the col
and row
fields of each term
, but you would need to pass the array length to the transpose
function as it cannot determine the number of elements from the argument types, values or contents.
Yet the main problem is you are not calling transpose
at all: void transpose(term a[], term b[]);
is just a function declaration, not a call. You should write:
// call the transpose function
transpose(a, b);
The example posted suggests you are dealing with simple matrices represented as arrays of arrays. here is a modified version for 3x3 matrices represented as arrays of arrays:
#include <stdio.h>
void transpose(int a[3][3], int b[3][3]) {
if (a == b) {
// transposing the matrix in place:
// swap the upper and lower triangles
for (int i = 0; i < 3; i++) {
for (int j = 0; j < i; j++) {
int temp = a[j][i];
a[j][i] = a[i][j];
a[i][j] = temp;
}
}
} else {
// source and destination are distinct
// copy the elements in transposed order
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
a[i][j] = b[j][i];
}
}
}
}
int main() {
int a[3][3] = { {1,2,3}, {4,5,6}, {7,8,9} }
int b[3][3];
// print term a
printf("original matrix\n");
for (int i = 0; i < 3; i++) {
printf(" %d %d %d\n", a[i][0], a[i][1], a[i][2]);
}
printf("\n");
// call the transpose function
transpose(a, b);
// print term b
printf("transpose matrix\n");
for (int i = 0; i < 3; i++) {
printf(" %d %d %d\n", b[i][0], b[i][1], b[i][2]);
}
return 0;
}