Skip to content
Snippets Groups Projects
Commit 9702e004 authored by dario.genga's avatar dario.genga
Browse files

Add command line execution

The program can now be executed by command line with the following
syntax :

./stack [array_length] [arguments]

This will sort the values passed as arguments and print them in the
console.

Also uptaded the README file with this new execution.
parent a0dc7c37
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,18 @@ ...@@ -11,6 +11,18 @@
Use this command to compile the project. 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 ### Clean the project
> `make clean` > `make clean`
...@@ -18,5 +30,7 @@ Use this command to clean the project. ...@@ -18,5 +30,7 @@ Use this command to clean the project.
### Run the tests ### Run the tests
> `make test` > `make test`
>
> `./test`
Use this command to start the tests. Use this command to compile and start the tests.
\ No newline at end of file
...@@ -6,8 +6,38 @@ ...@@ -6,8 +6,38 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "stack.h" #include "stack.h"
#define ARGS_MIN 2
#define ARG_INDEX_array_size 1
int main() {
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; return EXIT_SUCCESS;
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment