Untitled
plain_text
8 days ago
4.2 kB
0
Indexable
Never
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <time.h> // Function to generate a random relation matrix void generateRelationMatrix(int n, int** matrix) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { matrix[i][j] = rand() % 2; // Randomly assign 0 or 1 } } } // Function to check if a relation matrix is symmetric bool isSymmetric(int n, int** matrix) { for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (matrix[i][j] != matrix[j][i]) { return false; } } } return true; } // Function to check if a relation matrix is anti-symmetric bool isAntiSymmetric(int n, int** matrix) { for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (matrix[i][j] && matrix[j][i]) { return false; } } } return true; } // Function to check if a relation matrix is transitive bool isTransitive(int n, int** matrix) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (matrix[i][j]) { for (int k = 0; k < n; k++) { if (matrix[j][k] && !matrix[i][k]) { return false; } } } } } return true; } // Function to check if a relation matrix is an equivalence relation bool isEquivalence(int n, int** matrix) { return isSymmetric(n, matrix) && isTransitive(n, matrix); } // Function to check if a relation matrix represents a function bool isFunction(int n, int** matrix) { for (int i = 0; i < n; i++) { int count = 0; for (int j = 0; j < n; j++) { if (matrix[i][j]) { count++; } } if (count != 1) { return false; } } return true; } int main() { int n; printf("Enter the number of relation matrices: "); scanf("%d", &n); // Open files for storing relation matrices and generation times FILE* relationFile = fopen("relations.txt", "w"); FILE* timeFile = fopen("generation_times.txt", "w"); srand(time(NULL)); // Seed the random number generator clock_t start, end; double cpu_time_used; for (int i = 0; i < n; i++) { int size = rand() % 10 + 1; // Randomly generate matrix size between 1 and 10 // Allocate memory for the matrix int** matrix = (int**)malloc(size * sizeof(int*)); for (int j = 0; j < size; j++) { matrix[j] = (int*)malloc(size * sizeof(int)); } start = clock(); generateRelationMatrix(size, matrix); end = clock(); // Calculate generation time in milliseconds cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC * 1000; // Write the matrix to the file fprintf(relationFile, "Matrix %d:\n", i + 1); for (int j = 0; j < size; j++) { for (int k = 0; k < size; k++) { fprintf(relationFile, "%d ", matrix[j][k]); } fprintf(relationFile, "\n"); } fprintf(relationFile, "\n"); // Write the generation time to the file fprintf(timeFile, "Matrix %d: %.2f milliseconds\n", i + 1, cpu_time_used); // Verify properties of the matrix printf("Matrix %d:\n", i + 1); printf("Symmetric: %s\n", isSymmetric(size, matrix) ? "Yes" : "No"); printf("Anti-Symmetric: %s\n", isAntiSymmetric(size, matrix) ? "Yes" : "No"); printf("Transitive: %s\n", isTransitive(size, matrix) ? "Yes" : "No"); printf("Equivalence: %s\n", isEquivalence(size, matrix) ? "Yes" : "No"); printf("Function: %s\n", isFunction(size, matrix) ? "Yes" : "No"); printf("\n"); // Free memory for the matrix for (int j = 0; j < size; j++) { free(matrix[j]); } free(matrix); } // Close the files fclose(relationFile); fclose(timeFile); return 0; }