From acdfccd7c995de4b369c7016a827a61f4878d8d5 Mon Sep 17 00:00:00 2001 From: "dario.genga" <dario.genga@etu.hesge.ch> Date: Tue, 7 Dec 2021 16:02:08 +0100 Subject: [PATCH] Add non finished ex5 Something is wrong from the second value of the reversed array. --- ex5/main.c | 38 ++++++++++++++++++++++++++++++++++++++ ex5/makefile | 10 ++++++++++ 2 files changed, 48 insertions(+) create mode 100644 ex5/main.c create mode 100644 ex5/makefile diff --git a/ex5/main.c b/ex5/main.c new file mode 100644 index 0000000..122e1df --- /dev/null +++ b/ex5/main.c @@ -0,0 +1,38 @@ +/* Author : Dario GENGA + * Date : 07.12.2021 + * Description : ContrĂ´le continue 1 - Exercice 5 + */ + +#include <stdio.h> +#include <stdlib.h> +#define ARRAY_LENGTH 6 + +void reverse(double *array_from, double *array_to, int index) { + if (index >= 0) { + array_to[ARRAY_LENGTH - index - 1] = array_from[index]; + reverse(array_from, array_from, index - 1); + } +} + +int main() { + double *array = malloc(sizeof(double) * ARRAY_LENGTH); + double *result = malloc(sizeof(double) * ARRAY_LENGTH); + + for (int i = 0; i < ARRAY_LENGTH; i++) { + double value; + scanf("%lf", &value); + array[i] = value; + } + + reverse(array, result, ARRAY_LENGTH - 1); + + printf("\n"); + for (int i = 0; i < ARRAY_LENGTH; i++) { + printf("%lf\n", result[i]); + } + + free(array); + free(result); + + return EXIT_SUCCESS; +} diff --git a/ex5/makefile b/ex5/makefile new file mode 100644 index 0000000..a217aa2 --- /dev/null +++ b/ex5/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