-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1780 from rajdeepchakraborty-rc/magic
Added: Magic Square Program
- Loading branch information
Showing
2 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include <stdio.h> | ||
#include <stdbool.h> | ||
|
||
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; | ||
} |