From 509d69b94f3836aee92a0577cf032e487e8a9909 Mon Sep 17 00:00:00 2001 From: Rajdeep Chakraborty <68934988+rajdeepchakraborty-rc@users.noreply.github.com> Date: Fri, 8 Nov 2024 22:51:54 +0530 Subject: [PATCH] Added: Magic Square Program Added the Magic Square C program and README.md file --- 2D Arrays/Magic_Square/README.md | 75 ++++++++++++++++++++++++++ 2D Arrays/Magic_Square/magic_square.c | 76 +++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 2D Arrays/Magic_Square/README.md create mode 100644 2D Arrays/Magic_Square/magic_square.c diff --git a/2D Arrays/Magic_Square/README.md b/2D Arrays/Magic_Square/README.md new file mode 100644 index 00000000..66cfe2fc --- /dev/null +++ b/2D Arrays/Magic_Square/README.md @@ -0,0 +1,75 @@ +# Magic Square + +## Description + +This program checks whether a given square matrix is a Magic Square. A Magic Square is a square matrix in which the sum of every row, column, and both diagonals is the same. The program employs an algorithm to validate this property for any square matrix provided by the user. + +``` +Example: +Enter the size(between 1 and 10) of the matrix (n x n): 3 +Enter the elements of the matrix: +2 7 6 +9 5 1 +4 3 8 + +Output: +The matrix is a magic square. +``` + +### Problem Definition + +1. **Given**: +- A square matrix `matrix[n][n]` with user-defined size `n`. + +2. **Objective**: +- Determine if the matrix satisfies the magic square property: `The sums of each row, each column, and both diagonals must be equal.` + +### Algorithm Overview + +1. **Input Validation**: + - Ensure the matrix is square (`n x n`). + +2. **Diagonal Calculation and Check**: + - Compute the sums of the primary(`sumDiag1`) and secondary(`sumDiag2`) diagonals and ensure they are equal. + +3. **Row and Column Sum Check**: + - Calculate the sums for each row(`sumRow`) and column(`sumCol`) and compare them with either of the diagonal sum(`sumDiag1`/`sumDiag2`) to check if they are all equal. + +5. **Output Result**: + - If all checks pass, the matrix is a magic square; otherwise, it is not. + +### Key Features + +- Validates any `n x n` matrix for the magic square property. +- Modular design with functions for input, processing, and output. +- Handles edge cases, such as invalid matrix sizes or non-square matrices. + +### Time Complexity + +- **Best case**: `O(n²)`, where `n` is the size of the matrix (all elements are processed once). +- **Worst case**: `O(n²)`, as every row, column, and diagonal must be checked. + +### Space Complexity + +- `O(1)` (in-place calculations, no additional memory allocation beyond the matrix). + +## Implementation + +The implementation in C includes: + +1. **Input Handling**: + - Accept user input for the matrix size and its elements. + +2. **Functionality**: + - A function to compute the sums of rows, columns, and diagonals. + - Logical checks to compare all sums with the reference sum. + +3. **Output**: + - Displays whether the matrix is a magic square or not. + +## Usage + +- Compile the program using a C compiler (e.g., `gcc magic_square.c -o magic_square`). +- Run the program (`./magic_square`). +- Input the size of the matrix and its elements. +- Observe the output indicating whether the matrix is a magic square. \ No newline at end of file diff --git a/2D Arrays/Magic_Square/magic_square.c b/2D Arrays/Magic_Square/magic_square.c new file mode 100644 index 00000000..f9f2601e --- /dev/null +++ b/2D Arrays/Magic_Square/magic_square.c @@ -0,0 +1,76 @@ +#include +#include + +bool isMagicSquare(int matrix[][10], int n) { + int sumDiag1 = 0, sumDiag2 = 0; + + // Calculate the sum of the primary diagonal + for (int i = 0; i < n; i++) { + sumDiag1 += matrix[i][i]; + } + + // Calculate the sum of the secondary diagonal + for (int i = 0; i < n; i++) { + sumDiag2 += matrix[i][n - i - 1]; + } + + // If the diagonal sums do not match, it is not a magic square + if (sumDiag1 != sumDiag2) { + return false; + } + + // Compare row sums and column sums to the diagonal sum + for (int i = 0; i < n; i++) { + int sumRow = 0, sumCol = 0; + + for (int j = 0; j < n; j++) { + sumRow += matrix[i][j]; // Row Sum + sumCol += matrix[j][i]; // Column Sum + } + + if (sumRow != sumDiag1 || sumCol != sumDiag1) { + return false; + } + } + + return true; +} + +int main() { + int n; + + printf("Enter the size(between 1 and 10) of the matrix (n x n): "); + scanf("%d", &n); + + if (n <= 0 || n > 10) { + printf("Invalid size! Please enter a size between 1 and 10.\n"); + return 1; + } + + int matrix[10][10]; + + // Input array elements from user + printf("Enter the elements of the matrix:\n"); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + scanf("%d", &matrix[i][j]); + } + } + + // Print the matrix + printf("The elements of the matrix:\n"); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + printf("%d ", matrix[i][j]); + } + printf("\n"); + } + + if (isMagicSquare(matrix, n)) { + printf("The matrix is a magic square.\n"); + } else { + printf("The matrix is NOT a magic square.\n"); + } + + return 0; +}