#include <stdio.h>

#define MAX_TERMS 100

typedef struct {
    int row;
    int col;
    int value;
} Term;

void transpose(Term a[], Term b[]) {
    int n, i, j, currentb;

    n = a[0].value;  // Number of elements
    b[0].row = a[0].col;  // Number of columns in b = number of rows in a
    b[0].col = a[0].row;  // Number of rows in b = number of columns in a
    b[0].value = n;

    if (n > 0) {
        currentb = 1;
        for (i = 0; i < a[0].col; i++) {
            for (j = 1; j <= n; j++) {
                if (a[j].col == i) {
                    b[currentb].row = a[j].col;
                    b[currentb].col = a[j].row;
                    b[currentb].value = a[j].value;
                    currentb++;
                }
            }
        }
    }
}

void printMatrix(Term matrix[]) {
    printf("Row\tColumn\tValue\n");
    for (int i = 0; i <= matrix[0].value; i++) {
        printf("%d\t%d\t%d\n", matrix[i].row, matrix[i].col, matrix[i].value);
    }
}

int main() {
    Term a[MAX_TERMS], b[MAX_TERMS];

    // Initialize matrix a with user input
    int numElements;
    printf("Enter the number of non-zero elements: ");
    scanf("%d", &numElements);

    a[0].row = 6;  // Assuming a 6x6 matrix, can be changed if needed
    a[0].col = 6;
    a[0].value = numElements;

    printf("Enter the elements in the format 'row col value':\n");
    for (int i = 1; i <= numElements; i++) {
        scanf("%d %d %d", &a[i].row, &a[i].col, &a[i].value);
    }

    printf("Original Matrix:\n");
    printMatrix(a);

    transpose(a, b);

    printf("\nTransposed Matrix:\n");
    printMatrix(b);

    return 0;
}