From 9987eecde5d9d03302de9b9646918400039ac3fc Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 23 Sep 2024 16:26:50 -0500 Subject: [PATCH] Bonus is done --- BQ.c | 68 +++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/BQ.c b/BQ.c index 563104e..ec834b2 100644 --- a/BQ.c +++ b/BQ.c @@ -1,10 +1,13 @@ #include #include // Bonus Question -// All rows, columns, and diagonals are equal +/* Find if all rows, columns, and diagonals in squares have equals sums. + * Will create an array to hold sums for each row, column, and diagonal. Arr length is (2 * length) + 2 + * Then check if all sums in array are equal + */ -// Sum of each row, in arr +// Sum of each row in arr int find_row_sum(const int arr[], int len) { int sum = 0; for(int i = 0; i < len; i++) { @@ -12,16 +15,17 @@ int find_row_sum(const int arr[], int len) { } return sum; } -// Sum Of each column, in arr -int find_column_sum(const int arr[3][3],int row, int len) { +// Sum of each column in arr +int find_column_sum(int len, const int arr[len][len],int row) { int sum = 0; for(int i = 0; i < len; i++) { sum+=arr[row][i]; } return sum; } -// Sum of diagonals, in arr -int find_diagonal_sum(const int arr[3][3], int len) { + +// Function to find diagonal sum from top left to bottom right +int find_diagonal_sum(int len, const int arr[len][len]) { int sum = 0; int j = 0; for(int i = 0; i < len; i++) { @@ -30,7 +34,8 @@ int find_diagonal_sum(const int arr[3][3], int len) { } return sum; } -int find_diagonal_sum_the_other_way(const int arr[3][3], int len) { +// Function to find diagonal sum from bottom left to top right +int find_diagonal_sum_the_other_direction(int len, const int arr[len][len]) { int sum = 0; int j = len - 1; for(int i = 0; i < len; i++) { @@ -39,7 +44,7 @@ int find_diagonal_sum_the_other_way(const int arr[3][3], int len) { } return sum; } - +// Final function to check if all nums in sum are equal, aka is the square perfect bool is_perfect_square(const int arr[], int len) { bool is_perfect = true; for(int i = 0; i < len - 1 && is_perfect; i++) { @@ -50,28 +55,43 @@ bool is_perfect_square(const int arr[], int len) { } int main() { - // Input will be a set matrix for now - int M = 3; - int square[3][3] = {{8, 1, 6}, {3, 5, 7}, {4,9,2}}; + int M; // Length of squares sides + printf("Enter square length: "); + scanf("%d", &M); - int sums[2*M + 2]; - // MAKE THIS SINGLY LINKED LIST LATER + int square[M][M]; + + // Input values into matrix for(int i = 0; i < M; i++) { - sums[i] = find_row_sum(square[i], M); - } - for(int i = 0; i < M; i++) { - for (int j = M; j < 2 * M; j++) { - sums[j] = find_column_sum(square, i, M); + for(int j = 0; j < M; j++) { + printf("Enter matrix value %d, %d: ", i, j); + scanf("%d", &square[i][j]); } } - sums[2*M] = find_diagonal_sum(square, M); - sums[2*M+1] = find_diagonal_sum_the_other_way(square, M); - for(int i = 0; i < sizeof(sums)/sizeof(sums[0]); i++) { - printf("%d ", sums[i]); + // The array that will hold our calculated sums + int sums[2*M + 2]; + // Find sum for each row + for(int i = 0; i < M; i++) { + sums[i] = find_row_sum(square[i], M); + } + // Find sum for each column + for(int i = 0; i < M; i++) { + for (int j = M; j < 2 * M; j++) { + sums[j] = find_column_sum(M, square, i); + } + } + // Find first diagonal + sums[2*M] = find_diagonal_sum(M, square); + // Find second diagonal + sums[2*M+1] = find_diagonal_sum_the_other_direction(M, square); + // + if(is_perfect_square(sums, M)) { + printf("The matrix is a Magic Square"); + } + else { + printf("The matrix is not a Magic Square"); } - - printf("\n%d", is_perfect_square(sums, M)); return 0; } \ No newline at end of file