From de4b927b8e94fdeb0b23e2b0c77cea464a6e271d Mon Sep 17 00:00:00 2001 From: "dario.genga" <dario.genga@etu.hesge.ch> Date: Tue, 7 Dec 2021 15:38:56 +0100 Subject: [PATCH] Add ex2 Still missing some tests to determine if it's a latin square. --- ex2/main.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ ex2/makefile | 10 ++++++++++ 2 files changed, 66 insertions(+) create mode 100644 ex2/main.c create mode 100644 ex2/makefile diff --git a/ex2/main.c b/ex2/main.c new file mode 100644 index 0000000..c30a2fc --- /dev/null +++ b/ex2/main.c @@ -0,0 +1,56 @@ +/* Author : Dario GENGA + * Date : 07.12.2021 + * Description : Contrôle continue 1 - Exercice 2 + */ + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#define NB_ROW_AND_COL 4 + +int main() { + bool is_normalized = true; + char *normalized = "oui"; + char *not_normalized = "non"; + int (*square)[NB_ROW_AND_COL] = malloc(NB_ROW_AND_COL * sizeof(*square)); + + // i = row index, k = col index + for (int i = 0; i < NB_ROW_AND_COL; i++) { + for (int k = 0; k < NB_ROW_AND_COL; k++) { + int value; + scanf("%d", &value); + square[i][k] = value; + + if (value <= 0 || value > NB_ROW_AND_COL) { + is_normalized = false; + } + } + } + + // Verify if the square is normalized + int prev = square[0][0]; + for (int i = 1; i < NB_ROW_AND_COL; i++) { + if (prev > square[i][0]) { + is_normalized = false; + break; + } + } + prev = square[0][0]; + for (int k = 1; k < NB_ROW_AND_COL; k++) { + if (prev > square[0][k]) { + is_normalized = false; + break; + } + } + + // Print the result and free the memory + if (is_normalized) { + printf("\nlatin normalisé : %s\n", normalized); + } else { + printf("\nlatin normalisé : %s\n", not_normalized); + } + + free(square); + + return EXIT_SUCCESS; +} diff --git a/ex2/makefile b/ex2/makefile new file mode 100644 index 0000000..a217aa2 --- /dev/null +++ b/ex2/makefile @@ -0,0 +1,10 @@ +LIB=-lm +CC=gcc -Wall -Wextra -pedantic -g + +main: main.o + $(CC) $^ -fsanitize=address -fsanitize=leak -o $@ $(LIB) + +main.o: main.c + $(CC) -c $< $(LIB) +clean: + rm -f *.o main \ No newline at end of file -- GitLab