I've written a simple program using modular programming in C (using a lot of functions), that gets a line of input from the user, and then prints if a given input forms a magic square or not.
The first input is the order of the matrix, and the rest are order*order inputs(order^2)(integers) that are actually filling the matrix. So if I put for instance 3 1 2 3 4 5 6 7 8 9
the order will be 3 and the inputs will be 1,..,9
.
One thing that I am not sure is how to handle inputs less than 9.
Anyway, I would like to see your tweaks and learn more and improve from your blessed share of knowledge.
Please keep in mind it should be written in ANSI-C (I know it's old and all, but those are the requirements). If you have something cool to share regarding C11 or others, please do so as I like to extend my knowledge and improve (even though I'm a noob now).
#include <stdio.h>
#include <stdlib.h>
#define MAX 10 /*maximal size of the array*/
void checkOrderRange(int order, int matrix[order][order]); /* checking if number of rows+number of columns is greater than N^2 */
void checkOrderType(int order); /*checking if order is a digit*/
void checkOrderSize(int order); /*checking if order is in the correct size (bigger than 3 and smaller than selected MAX) */
void scanMatrix(int order, int matrix[order][order]); /*scans the matrix and assigns given values */
void printMatrix(int order, int matrix[order][order]); /*prints given matrix*/
int diagonalSumI(int order, int matrix[order][order]); /*calculates first diagonal sum */
int diagonalSumII(int order, int matrix[order][order]); /*calculates second diagonal sum */
int columnSumAndCompareToDiagonal(int order, int matrix[order][order]); /*calculates sum of each coloumn and stores in an array*/
int rowSumAndCompareToDiagonal(int order, int matrix[order][order]); /*calculates sum of each row and stores in an array*/
void checkSum(int order, int matrix[order][order]); /*compares above sums to check if it's indeed a magic square*/
int isdigit();
void checkIfMagicSquare(int order, int matrix[order][order]);
int main() {
int order, matrix[MAX][MAX];
printf("please enter the order of the magic square \n");
scanf("%d", &order);
scanMatrix(order, matrix);
checkOrderSize(order);
printf("here's your matrix \n");
printMatrix(order, matrix);
columnSumAndCompareToDiagonal(order, matrix);
rowSumAndCompareToDiagonal(order, matrix);
checkIfMagicSquare(order,matrix);
return 0;
}
void scanMatrix(int order, int matrix[order][order]) {
for (int I = 0; I < order; I++) {
for (int J = 0; J < order; J++) {
if (scanf("%d", &matrix[I][J]) != 1) {
puts("you did not enter an integer(decimal)\n"); /* checking for integer insertion*/
exit(0);
}
}
}
}
void printMatrix(int order, int matrix[order][order]) {
for (int I = 0; I < order; I++) {
for (int J = 0; J < order; J++) {
printf("%d\t", matrix[I][J]);
}
printf("\n");
}
}
int diagonalSumI(int order, int matrix[order][order]) {
int diagonal = 0;
for (int I = 0; I < order; I++) {
diagonal = diagonal + matrix[I][I];
}
return diagonal;
}
int diagonalSumII(int order, int matrix[order][order]) {
int diagonal2 = 0;
for (int I = 0, J = order - 1; I < order; I++, J--) {
diagonal2 = diagonal2 + matrix[I][J];
}
return diagonal2;
}
int columnSumAndCompareToDiagonal(int order, int matrix[order][order]) {
int columnsum[order];
for (int I = 0; I < order; I++) {
columnsum[I] = 0;
for (int J = 0; J < order; J++) {
columnsum[I] = columnsum[I] + matrix[I][J];
}
}
for (int K = 0; K < order; K++) {
if (columnsum[K] == diagonalSumI(order, matrix) && diagonalSumI(order, matrix) == diagonalSumII(order, matrix)) {}
else {
fprintf(stderr, "Sorry. but not a magic square\n");
exit(EXIT_FAILURE);
return 0;
}
}
return 1;
}
int rowSumAndCompareToDiagonal(int order, int matrix[order][order]) {
int rowsum[order];
for (int J = 0; J < order; J++) {
rowsum[J] = 0;
for (int I = 0; I < order; I++) {
rowsum[J] = rowsum[J] + matrix[I][J];
}}
for (int K = 0; K < order; K++) {
if (rowsum[K] == diagonalSumI(order, matrix) && diagonalSumI(order, matrix) == diagonalSumII(order, matrix)) {
} else {
fprintf(stderr, "Sorry. but not a magic square\n");
exit(EXIT_FAILURE);
return 0;
}
}
return 1;
}
void checkIfMagicSquare(int order, int matrix[order][order]) {
if (rowSumAndCompareToDiagonal(order, matrix) && columnSumAndCompareToDiagonal(order, matrix)) {
printf("congratulations, it is a magic square \n");
} else {
fprintf(stderr, "Sorry. but not a magic square \n");
}
}
void checkOrderSize(int order) { /*checking if order is in the correct size (bigger than 3 and smaller than selected MAX) */
if (order < 3 || order > MAX) {
fprintf(stderr, "order of matrix is either smaller than 3 or larger than %d\n", MAX);
exit(EXIT_FAILURE);
}
}
void checkOrderType(int order) { /*checking if order is a digit*/
if (!isdigit(order)) {
fprintf(stderr, "Input is not decimal or not a digit\n");
exit(EXIT_FAILURE);
}
}
for (int I = …;
declarations are currently not ANSI C compliant. \$\endgroup\$ – 200_success Dec 5 '17 at 14:23