|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#define MAX_MATRIX_SIZE 100 |
| 6 | + |
| 7 | +void transpose_matrix(int *matrix, int rows, int cols, int *transposed) { |
| 8 | + for (int i = 0; i < rows; i++) { |
| 9 | + for (int j = 0; j < cols; j++) { |
| 10 | + transposed[j * rows + i] = matrix[i * cols + j]; |
| 11 | + } |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +int main(int argc, char *argv[]) { |
| 16 | + if (argc != 4) { |
| 17 | + printf("Usage: please enter the dimension of the matrix and the serialized matrix\n"); |
| 18 | + return 1; |
| 19 | + } |
| 20 | + |
| 21 | + int cols = atoi(argv[1]); |
| 22 | + int rows = atoi(argv[2]); |
| 23 | + char *input = argv[3]; |
| 24 | + |
| 25 | + if (cols <= 0 || rows <= 0 || strlen(input) == 0) { |
| 26 | + printf("Usage: please enter the dimension of the matrix and the serialized matrix\n"); |
| 27 | + return 1; |
| 28 | + } |
| 29 | + |
| 30 | + int matrix[MAX_MATRIX_SIZE]; |
| 31 | + int transposed[MAX_MATRIX_SIZE]; |
| 32 | + int count = 0; |
| 33 | + char *token = strtok(input, ", "); |
| 34 | + |
| 35 | + while (token != NULL && count < rows * cols) { |
| 36 | + matrix[count++] = atoi(token); |
| 37 | + token = strtok(NULL, ", "); |
| 38 | + } |
| 39 | + |
| 40 | + if (count != rows * cols) { |
| 41 | + printf("Usage: please enter the dimension of the matrix and the serialized matrix\n"); |
| 42 | + return 1; |
| 43 | + } |
| 44 | + |
| 45 | + transpose_matrix(matrix, rows, cols, transposed); |
| 46 | + |
| 47 | + for (int i = 0; i < cols * rows; i++) { |
| 48 | + printf("%d", transposed[i]); |
| 49 | + if (i < cols * rows - 1) { |
| 50 | + printf(", "); |
| 51 | + } |
| 52 | + } |
| 53 | + printf("\n"); |
| 54 | + |
| 55 | + return 0; |
| 56 | +} |
0 commit comments