Untitled
unknown
c_cpp
2 years ago
3.9 kB
7
Indexable
#include <stdio.h> #include <stdlib.h> // Function to initialize the matrix with user input void initialize(int ***M, int *size) { int i, j; // Input matrix dimension scanf("%d", size); // Allocate memory for the matrix *M = (int **)malloc(*size * sizeof(int *)); for (i = 0; i < *size; i++) { (*M)[i] = (int *)malloc(*size * sizeof(int)); } // Input matrix elements for (i = 0; i < *size; i++) { for (j = 0; j < *size; j++) { scanf(" %d", &(*M)[i][j]); } } } // Function to output the matrix with size✕size 2D format void outputMatrix(const int **M, const int size) { int i, j; for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { printf("%d ", M[i][j]); } printf("\n"); } } // Function to rotate the matrix 90 degrees clockwise void rotate(int **M, const int size) { int i, j; int **temp; // Allocate memory for a temporary matrix temp = (int **)malloc(size * sizeof(int *)); for (i = 0; i < size; i++) { temp[i] = (int *)malloc(size * sizeof(int)); } // Copy the transpose of the matrix to the temporary matrix for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { temp[i][j] = M[j][i]; } } // Copy the rotated matrix back to the original matrix for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { M[i][j] = temp[i][j]; } } // Free the memory used by the temporary matrix for (i = 0; i < size; i++) { free(temp[i]); } free(temp); } // Function to check whether the matrix is symmetric int symValid(const int **M, const int size) { int i, j; // Check if M[i][j] is equal to M[j][i] for all i and j for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { if (M[i][j] != M[j][i]) { return 0; // Not symmetric } } } return 1; // Symmetric } // Function to perform matrix multiplication int** mul(const int **M, const int size) { int i, j, k; int **result; // Allocate memory for the result matrix result = (int **)malloc(size * sizeof(int *)); for (i = 0; i < size; i++) { result[i] = (int *)malloc(size * sizeof(int)); } // Perform matrix multiplication for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { result[i][j] = 0; for (k = 0; k < size; k++) { result[i][j] += M[i][k] * M[k][j]; } } } return result; } int main() { int size; int **M, **Msq, **rotatedM; initialize(&M, &size); // Create a copy of the original matrix for rotation rotatedM = (int **)malloc(size * sizeof(int *)); for (int i = 0; i < size; i++) { rotatedM[i] = (int *)malloc(size * sizeof(int)); for (int j = 0; j < size; j++) { rotatedM[i][j] = M[i][j]; } } // Rotate the matrix rotate(rotatedM, size); outputMatrix((const int **)rotatedM, size); // Cast to const int ** if (symValid((const int **)rotatedM, size)) // Cast to const int ** printf("Symmetric\n"); else printf("Not symmetric\n"); // Perform matrix multiplication using the original matrix Msq = mul((const int **)M, size); // Cast to const int ** outputMatrix((const int **)Msq, size); // Cast to const int ** // Free memory for (int i = 0; i < size; i++) { free(rotatedM[i]); } free(rotatedM); for (int i = 0; i < size; i++) { free(M[i]); free(Msq[i]); } free(M); free(Msq); return 0; }
Editor is loading...
Leave a Comment