Finished perfect square checks. Now to pretty it

This commit is contained in:
2024-09-17 15:43:03 -05:00
parent 5584727862
commit 8179bad76e

28
BQ.c
View File

@@ -1,9 +1,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdbool.h>
// Bonus Question // Bonus Question
// All rows, columns, and diagonals are equal // All rows, columns, and diagonals are equal
// Matrix MxM
// arr[M] sums * 2 + 2
// Sum of each row, in arr // Sum of each row, in arr
int find_row_sum(const int arr[], int len) { int find_row_sum(const int arr[], int len) {
int sum = 0; int sum = 0;
@@ -30,6 +30,24 @@ int find_diagonal_sum(const int arr[3][3], int len) {
} }
return sum; return sum;
} }
int find_diagonal_sum_the_other_way(const int arr[3][3], int len) {
int sum = 0;
int j = len - 1;
for(int i = 0; i < len; i++) {
sum += arr[i][j];
j--;
}
return sum;
}
bool is_perfect_square(const int arr[], int len) {
bool is_perfect = true;
for(int i = 0; i < len - 1 && is_perfect; i++) {
if(arr[i] != arr[i+1])
is_perfect = false;
}
return is_perfect;
}
int main() { int main() {
// Input will be a set matrix for now // Input will be a set matrix for now
@@ -47,11 +65,13 @@ int main() {
} }
} }
sums[2*M + 1] = find_diagonal_sum(square) 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++) { for(int i = 0; i < sizeof(sums)/sizeof(sums[0]); i++) {
printf("%d ", sums[i]); printf("%d ", sums[i]);
} }
printf("\n%d", is_perfect_square(sums, M));
return 0; return 0;
} }