From 44cced845456423463eb540eda6d695bc7d6fd7f Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Tue, 3 Sep 2024 01:58:24 +0530 Subject: [PATCH] Upload files to "DS/C/Lab/Week4" --- DS/C/Lab/Week4/transpose.c | 67 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 DS/C/Lab/Week4/transpose.c diff --git a/DS/C/Lab/Week4/transpose.c b/DS/C/Lab/Week4/transpose.c new file mode 100644 index 0000000..14fbc08 --- /dev/null +++ b/DS/C/Lab/Week4/transpose.c @@ -0,0 +1,67 @@ +#include + +#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; +} \ No newline at end of file