50 lines
1.1 KiB
C
50 lines
1.1 KiB
C
#include <stdio.h>
|
|
|
|
#define MAX_TERMS 100
|
|
#define MAX_COL 100
|
|
|
|
struct Element {
|
|
int row, col, value;
|
|
};
|
|
|
|
void fastTranspose(struct Element *a, struct Element *b, int m, int n, int num) {
|
|
int rowTerms[MAX_COL] = {0};
|
|
int rowStart[MAX_COL] = {0};
|
|
int i, j;
|
|
|
|
for (i = 0; i < num; i++)
|
|
rowTerms[a[i].col]++;
|
|
|
|
for (i = 1; i < n; i++)
|
|
rowStart[i] = rowStart[i-1] + rowTerms[i-1];
|
|
|
|
for (i = 0; i < num; i++) {
|
|
j = rowStart[a[i].col]++;
|
|
b[j].row = a[i].col;
|
|
b[j].col = a[i].row;
|
|
b[j].value = a[i].value;
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
struct Element a[MAX_TERMS] = {{0, 1, 3}, {1, 2, 5}, {2, 0, 2}, {3, 1, 4}};
|
|
struct Element b[MAX_TERMS];
|
|
int m = 4, n = 3, num = 4;
|
|
|
|
printf("The Original Matrix is:\n\n");
|
|
printf("row col val\n");
|
|
for (int i = 0; i < num; i++)
|
|
printf("%d\t%d\t%d\n", a[i].row, a[i].col, a[i].value);
|
|
|
|
printf("\n\n");
|
|
fastTranspose(a, b, m, n, num);
|
|
|
|
printf("The Transpose Matrix is:\n\n");
|
|
printf("row col val\n");
|
|
for (int i = 0; i < num; i++)
|
|
printf("%d\t%d\t%d\n", b[i].row, b[i].col, b[i].value);
|
|
|
|
return 0;
|
|
}
|
|
|