APL/APL1.1/TP12/magique.c
2021-10-11 16:58:53 +02:00

59 lines
1.4 KiB
C

#include<stdio.h>
#include<stdlib.h>
//Incomplet.
void show2dtable(int l, int c, int table[][5]) {
for (int line = 0; line < (l*2)+1; line++) {
if (line == 0 || line == (l*2)+1 || line % 2 == 0) {
for (int col = 0; col < c; col++) printf("+-----");
printf("+\n");
} else {
for (int col = 0; col < c; col++) printf("| %3d ", table[line/2][col]);
printf("|\n");
}
}
}
int main(int argc, char * argv[]) {
int numcheck[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int carre[3][3] = {};
int nb_val = 0;
do {
int val;
printf("Veuillez entrer la valeur n°%d : ", nb_val+1);
int result = scanf("%d", &val);
if (result == 1) {
carre[nb_val/3][nb_val%3] = val;
nb_val++;
}
} while (nb_val < 9);
int anchor = carre[0][0] + carre[0][1] + carre[0][2];
for (int i = 0; i < 7; i++) {
int condition_respected = 0;
if (i < 3) {
condition_respected = carre[i][0] + carre[i][1] + carre[i][2] == anchor;
} else if (i < 6) {
condition_respected = carre[0][i-3] + carre[1][i-3] + carre[2][i-3] == anchor;
} else if (i == 6) {
condition_respected = carre[0][0] + carre[1][1] + carre[2][2] == anchor;
} else {
condition_respected = carre[2][0] + carre[1][1] + carre[0][2] == anchor;
}
if (!condition_respected) {
printf("Ceci n'est pas un carré magique.\n");
return EXIT_SUCCESS;
}
}
printf("Votre carré est magique !\n");
return EXIT_SUCCESS;
}