diff --git a/README.md b/README.md index 0e246277ed39bfa0002af7959faa2a5920f04a91..a6b6ad8a7342aac1d75b68502ae027448db5add4 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,18 @@ Use this command to compile the project. +### Run the program + +Execute the file _stack_ to start the program. + +The program must have the following arguments : + +>./stack [array_length] [elements] + +Example : + +>./stack 5 0 -1 2 7 4 + ### Clean the project > `make clean` @@ -18,5 +30,7 @@ Use this command to clean the project. ### Run the tests > `make test` +> +> `./test` -Use this command to start the tests. \ No newline at end of file +Use this command to compile and start the tests. diff --git a/main.c b/main.c index 5d111c239522692e6fa6ce624d01fedb261c64e1..f6ca00e5498830299b99531d9e94480f296b58e7 100644 --- a/main.c +++ b/main.c @@ -6,8 +6,38 @@ #include <stdio.h> #include <stdlib.h> #include "stack.h" +#define ARGS_MIN 2 +#define ARG_INDEX_array_size 1 -int main() { - return EXIT_SUCCESS; +void print_array(int *array, int array_size) { + for (int i = 0; i < array_size; i++) { + printf("%d ", array[i]); + } + printf("\n"); } + +int main(int argc, char **argv) { + if (argc < ARGS_MIN) { + printf("Missing arguments.\n"); + return EXIT_FAILURE; + } + + int array_size = atoi(argv[ARG_INDEX_array_size]); + + if (array_size != argc - ARGS_MIN || array_size == 0) { + printf("The number of element must correspond to the size of the array.\n"); + return EXIT_FAILURE; + } + int *array = malloc(array_size * sizeof(int)); + + for (int i = 0; i < argc - ARGS_MIN; i++) { + array[i] = atoi(argv[i + ARGS_MIN]); + } + int* sorted_array = sort(array, array_size); + print_array(sorted_array, array_size); + + free(sorted_array); + free(array); + return EXIT_SUCCESS; +} \ No newline at end of file