From 1024e6f8dc342492740fbd7c09f5ca705c542630 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 14 Nov 2018 16:21:26 -0300 Subject: [PATCH 1/8] add check to malloc calls --- src/count_sort.c | 8 ++++++++ src/sort.c | 12 ++++++++++++ test/test.c | 12 ++++++++++++ 3 files changed, 32 insertions(+) diff --git a/src/count_sort.c b/src/count_sort.c index 43d121c..ea4de45 100644 --- a/src/count_sort.c +++ b/src/count_sort.c @@ -25,7 +25,15 @@ void count_sort(int *array, int n) { int i; int j; int *temp = malloc(sizeof(int) * n); + if (temp == NULL) { + fprintf("Error: Out of heap space!\n"); + exit(5); + } int *count = malloc(sizeof(int) * n); + if (count == NULL) { + fprintf("Error: Out of heap space!\n"); + exit(5); + } memcpy(temp, array, sizeof(int) * n); for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { diff --git a/src/sort.c b/src/sort.c index 59b6c42..6adff95 100644 --- a/src/sort.c +++ b/src/sort.c @@ -81,6 +81,10 @@ int read_buffer(int *variable) { } } char **check = malloc(sizeof(char**)); + if (check == NULL) { + fprintf("Error: Out of heap space!\n"); + exit(5); + } long input = strtol(buffer, check, 10); if (*check[0] == '\0') { free(check); @@ -197,7 +201,15 @@ int main (int argc, char **argv) { } unordered_array = malloc(sizeof(int) * n); + if (unordered_array == NULL) { + fprintf("Error: Out of heap space!\n"); + exit(5); + } work_array = malloc(sizeof(int) * n); + if (work_array == NULL) { + fprintf("Error: Out of heap space!\n"); + exit(5); + } atexit(cleanup); // Llenar el array con valores para ordenar después diff --git a/test/test.c b/test/test.c index 048c9f5..e874328 100644 --- a/test/test.c +++ b/test/test.c @@ -57,8 +57,20 @@ int main(int argc, char **argv) { int failed = 0; test_case = malloc(sizeof(int) * n); + if (test_case == NULL) { + fprintf("Error: Out of heap space!\n"); + exit(1); + } qarray = malloc(sizeof(int) * n); + if (qarray == NULL) { + fprintf("Error: Out of heap space!\n"); + exit(1); + } test_array = malloc(sizeof(int) * n); + if (test_array == NULL) { + fprintf("Error: Out of heap space!\n"); + exit(1); + } atexit(cleanup); From 74b9b9131c8ae6fbc6438b7c171de8151b9157a3 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 14 Nov 2018 16:32:07 -0300 Subject: [PATCH 2/8] update readme information --- README.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d034e72..b5f83e9 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,20 @@ # Sort -Una programa que deja ejecutar varios algoritmos de ordenamiento. \ No newline at end of file +Una programa que deja ejecutar varios algoritmos de ordenamiento con el próposito de de comprobar la diferencia en rendamiento de los algoritmos. + +## Autores +Christopher Cromer + +Rodolfo Cuevas + +## Compilar +Hay tres blancos disponible para usar con GNU Make: + + make sort +Compilar el programa + + make test +Comprobar que los algoritmos y programa corren como esperado + + make informe +Compilar el informe si pdflatex está disponible en el sistema + From 0fa4c08a72e7bf6f8a7486cdc05a5feec460f422 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 14 Nov 2018 17:53:45 -0300 Subject: [PATCH 3/8] implement merge sort --- Makefile | 2 +- src/include/merge_sort.h | 21 ++++++++++++ src/merge_sort.c | 74 ++++++++++++++++++++++++++++++++++++++++ src/sort.c | 10 +++++- test/Makefile | 2 +- test/test.c | 20 +++++++++++ 6 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 src/include/merge_sort.h create mode 100644 src/merge_sort.c diff --git a/Makefile b/Makefile index 32a33cf..82f9228 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC=gcc CFLAGS=-Wall -Isrc/include -DDEBUG -g #LDFLAGS=-lm -SRC=src/sort.c src/random.c src/bubble_sort.c src/timer.c src/count_sort.c src/quick_sort.c +SRC=src/sort.c src/random.c src/bubble_sort.c src/timer.c src/count_sort.c src/quick_sort.c src/merge_sort.c OBJ=$(SRC:.c=.o) all: sort informe diff --git a/src/include/merge_sort.h b/src/include/merge_sort.h new file mode 100644 index 0000000..1701ff0 --- /dev/null +++ b/src/include/merge_sort.h @@ -0,0 +1,21 @@ +/* + * Copyright 2018 Christopher Cromer + * Copyright 2018 Rodolfo Cuevas + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _SORT_MERGE + #define _SORT_MERGE + void merge_sort(int *array, int n); + void merge_sort_run(int *array, int *temp, int left, int right); + void merge(int *array, int *temp, int prev_left, int prev_middle, int right); +#endif diff --git a/src/merge_sort.c b/src/merge_sort.c new file mode 100644 index 0000000..fd91065 --- /dev/null +++ b/src/merge_sort.c @@ -0,0 +1,74 @@ +/* + * Copyright 2018 Christopher Cromer + * Copyright 2018 Rodolfo Cuevas + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include "merge_sort.h" + +/** + * Usar el algoritmo de merge sort + * @param array El array a ordenar + * @param n El tamaño del array + */ +void merge_sort(int *array, int n) { + int *temp = malloc(sizeof(int)* n); + merge_sort_run(array, temp, 0, n - 1); + free(temp); +} + +/** + * Correro el merge sort recursivamente + * @param array El array a ordenar + * @param left El lado izquerda a ordenar + * @param right El lado derercha a ordenar + */ +void merge_sort_run(int *array, int *temp, int left, int right) { + if (left != right) { + int middle = (left + right) / 2; + merge_sort_run(array, temp, left, middle); + merge_sort_run(array, temp, middle + 1, right); + merge(array, temp, left, middle + 1, right); + } +} + +/** + * Unir los resultados de "dividir y conquistar" de merge sort + */ +void merge(int *array, int *temp, int prev_left, int prev_middle, int right) { + int i = 0; + int left = prev_left; + int middle = prev_middle - 1; + int far_right = right - left + 1; + + while (prev_left <= middle && prev_middle <= right) { + if (array[prev_left] < array[prev_middle]) { + temp[i++] = array[prev_left++]; + } + else { + temp[i++] = array[prev_middle++]; + } + } + + while (prev_left <= middle) { + temp[i++] = array[prev_left++]; + } + + while (prev_middle <= right) { + temp[i++] = array[prev_middle++]; + } + + for (i = 0; i < far_right; i++) { + array[left + i] = temp[i]; + } +} diff --git a/src/sort.c b/src/sort.c index 59b6c42..e979a72 100644 --- a/src/sort.c +++ b/src/sort.c @@ -22,6 +22,7 @@ #include "bubble_sort.h" #include "count_sort.h" #include "quick_sort.h" +#include "merge_sort.h" #define SORT_VERSION "1.0.0" @@ -217,7 +218,14 @@ int main (int argc, char **argv) { } if (merge) { - // merge sort + fprintf(stdout, "Merge sort corriendo... "); + fflush(stdout); + memcpy(work_array, unordered_array, sizeof(int) * n); + start_timer(); + merge_sort(work_array, n); + stop_timer(); + fprintf(stdout, "done\n"); + print_timer(); } if (quick) { diff --git a/test/Makefile b/test/Makefile index 55c22d0..b07c0b2 100644 --- a/test/Makefile +++ b/test/Makefile @@ -2,7 +2,7 @@ CC=gcc CFLAGS=-Wall -I../src/include -DDEBUG -g SRC=test.c OBJ=$(SRC:.c=.o) -OBJ+=../src/random.o ../src/bubble_sort.o ../src/timer.o ../src/count_sort.o ../src/quick_sort.o +OBJ+=../src/random.o ../src/bubble_sort.o ../src/timer.o ../src/count_sort.o ../src/quick_sort.o ../src/merge_sort.o all: test diff --git a/test/test.c b/test/test.c index 048c9f5..1e9142b 100644 --- a/test/test.c +++ b/test/test.c @@ -21,6 +21,7 @@ #include "bubble_sort.h" #include "count_sort.h" #include "quick_sort.h" +#include "merge_sort.h" static int *test_case; static int *test_array; @@ -146,6 +147,25 @@ int main(int argc, char **argv) { passed++; } + // Test merge sort + pass = 1; + memcpy(test_array, test_case, sizeof(int) * n); + fprintf(stdout, "\tmerge sort: "); + fflush(stdout); + merge_sort(test_array, n); + for (i = 0; i < n; i++) { + if (test_array[i] != qarray[i]) { + fprintf(stdout, "fail\n"); + failed++; + pass = 0; + break; + } + } + if (pass) { + fprintf(stdout, "pass\n"); + passed++; + } + fprintf(stdout, "%d tests passed\n", passed); fprintf(stdout, "%d tests failed\n", failed); From 0875b5ffbaea0f01a7934cb320f364aeb72b713a Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 14 Nov 2018 17:57:01 -0300 Subject: [PATCH 4/8] add missing doc --- src/merge_sort.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/merge_sort.c b/src/merge_sort.c index fd91065..e4ddbbb 100644 --- a/src/merge_sort.c +++ b/src/merge_sort.c @@ -30,6 +30,7 @@ void merge_sort(int *array, int n) { /** * Correro el merge sort recursivamente * @param array El array a ordenar + * @param temp Un array temporario para trabajar * @param left El lado izquerda a ordenar * @param right El lado derercha a ordenar */ @@ -44,6 +45,11 @@ void merge_sort_run(int *array, int *temp, int left, int right) { /** * Unir los resultados de "dividir y conquistar" de merge sort + * @param array El array a ordenar + * @param temp Un array temporario para trabajar + * @param prev_left El lado izquerda anterior + * @param prev_middle La particion de medio anterior + * @param right El lado derecha */ void merge(int *array, int *temp, int prev_left, int prev_middle, int right) { int i = 0; From d0b5e27c17a63eaea266f6c19ee8860eba98b3c1 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 14 Nov 2018 18:06:41 -0300 Subject: [PATCH 5/8] clean up quick sort source files --- src/quick_sort.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/quick_sort.c b/src/quick_sort.c index aa56d78..95a0274 100644 --- a/src/quick_sort.c +++ b/src/quick_sort.c @@ -13,22 +13,30 @@ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +/** + * Usar el algoritmo de quick sort + * @param array El array a ordenar + * @param n El tamaño del array + */ void quick_sort(int *array, int n) { - if(n<2)return; + if(n < 2) { + return; + } int i; int j; int temp; - int pivote=array[n/2]; - for(i=0,j=n-1;;i++,j--){ - while(array[i] < pivote) i++; - while(array[j] > pivote) j--; - if(i>=j)break; - temp=array[i]; + int pivote = array[n / 2]; + for (i =0, j = n - 1;; i++, j--) { + while (array[i] < pivote) i++; + while (array[j] > pivote) j--; + if (i >= j) { + break; + } + temp = array[i]; array[i] = array[j]; - array[j] = temp; + array[j] = temp; } - quick_sort(array,i); - quick_sort(array+i,n-i); + quick_sort(array, i); + quick_sort(array + i, n - i); } - From 9980b9491b63f068b4da487320599f4cbe9df31e Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 14 Nov 2018 20:17:41 -0300 Subject: [PATCH 6/8] fix fprintf --- src/count_sort.c | 5 +++-- src/sort.c | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/count_sort.c b/src/count_sort.c index ea4de45..0135485 100644 --- a/src/count_sort.c +++ b/src/count_sort.c @@ -13,6 +13,7 @@ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include @@ -26,12 +27,12 @@ void count_sort(int *array, int n) { int j; int *temp = malloc(sizeof(int) * n); if (temp == NULL) { - fprintf("Error: Out of heap space!\n"); + fprintf(stderr, "Error: Out of heap space!\n"); exit(5); } int *count = malloc(sizeof(int) * n); if (count == NULL) { - fprintf("Error: Out of heap space!\n"); + fprintf(stderr, "Error: Out of heap space!\n"); exit(5); } memcpy(temp, array, sizeof(int) * n); diff --git a/src/sort.c b/src/sort.c index 650a598..231b5c0 100644 --- a/src/sort.c +++ b/src/sort.c @@ -83,7 +83,7 @@ int read_buffer(int *variable) { } char **check = malloc(sizeof(char**)); if (check == NULL) { - fprintf("Error: Out of heap space!\n"); + fprintf(stderr, "Error: Out of heap space!\n"); exit(5); } long input = strtol(buffer, check, 10); @@ -203,12 +203,12 @@ int main (int argc, char **argv) { unordered_array = malloc(sizeof(int) * n); if (unordered_array == NULL) { - fprintf("Error: Out of heap space!\n"); + fprintf(stderr, "Error: Out of heap space!\n"); exit(5); } work_array = malloc(sizeof(int) * n); if (work_array == NULL) { - fprintf("Error: Out of heap space!\n"); + fprintf(stderr, "Error: Out of heap space!\n"); exit(5); } atexit(cleanup); From a04bf8a8e0c2cdb4a5e8eca8743dbc45266401f1 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 14 Nov 2018 20:21:15 -0300 Subject: [PATCH 7/8] fix fprintf in tests --- test/test.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test.c b/test/test.c index d8acd3b..5cb08aa 100644 --- a/test/test.c +++ b/test/test.c @@ -59,17 +59,17 @@ int main(int argc, char **argv) { test_case = malloc(sizeof(int) * n); if (test_case == NULL) { - fprintf("Error: Out of heap space!\n"); + fprintf(stderr, "Error: Out of heap space!\n"); exit(1); } qarray = malloc(sizeof(int) * n); if (qarray == NULL) { - fprintf("Error: Out of heap space!\n"); + fprintf(stderr, "Error: Out of heap space!\n"); exit(1); } test_array = malloc(sizeof(int) * n); if (test_array == NULL) { - fprintf("Error: Out of heap space!\n"); + fprintf(stderr, "Error: Out of heap space!\n"); exit(1); } From bdb847e6d902544bd0553ea80b811479f3ff2944 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Fri, 16 Nov 2018 13:25:47 -0300 Subject: [PATCH 8/8] check input of n and values from stdin --- src/sort.c | 116 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 95 insertions(+), 21 deletions(-) diff --git a/src/sort.c b/src/sort.c index 231b5c0..04d5705 100644 --- a/src/sort.c +++ b/src/sort.c @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include "random.h" #include "timer.h" #include "bubble_sort.h" @@ -27,9 +29,12 @@ #define SORT_VERSION "1.0.0" /** - * El array a ordenar + * El array desordenado */ static int *unordered_array; +/** + * El array a ordenar + */ static int *work_array; /** @@ -72,30 +77,63 @@ void print_array(int *array, int n) { * @return Retorna 1 si es exitosa ó 0 si falla */ int read_buffer(int *variable) { - char buffer[12]; + char buffer[32]; + char *check; while (1) { - if (fgets(buffer, 12, stdin) != NULL) { + if (fgets(buffer, 32, stdin) != NULL) { if (buffer[strlen(buffer) - 1] == '\n') { buffer[strlen(buffer) - 1] = '\0'; break; } } } - char **check = malloc(sizeof(char**)); - if (check == NULL) { - fprintf(stderr, "Error: Out of heap space!\n"); - exit(5); + errno = 0; + long input = strtol(buffer, &check, 10); + if (buffer == check) { + // Empty + return 0; } - long input = strtol(buffer, check, 10); - if (*check[0] == '\0') { - free(check); + else if (errno == ERANGE && input == LONG_MIN) { + // Overflow + return 0; + } + else if (errno == ERANGE && input == LONG_MAX) { + // Underflow + return 0; + } + else if (errno == EINVAL) { /* not in all c99 implementations - gcc OK */ + // Base contains unsupported value + // This check is not in all c99 implementations, but does exist in gcc + return 0; + } + else if (errno != 0 && input == 0) { + // Unspecified error + return 0; + } + else if (errno == 0 && !*check) { + // Valid number + if (input > INT_MAX || input < INT_MIN) { + fprintf(stderr, "Error: n tiene que ser menor de 2147483648 y mayor de -2147483649!\n"); + return 0; + } *variable = (int) input; return 1; } - else { - free(check); + else if (errno == 0 && *check != 0) { + // Contains non number characters return 0; } + else { + return 0; + } +} + +/** + * Imprimir un mensaje y salir si n es invalido + */ +void print_invalid_n() { + fprintf(stderr, "Error: El valor de n es invalido!\n"); + exit(7); } /** @@ -112,7 +150,9 @@ void cleanup() { * @return Retorna el codigo de error o 0 por exito */ int main (int argc, char **argv) { - long long i; + char *check = NULL; + long ninput = 0; + int i; int n = 10; int elegir = 0; int imprimir = 0; @@ -173,10 +213,44 @@ int main (int argc, char **argv) { selection = 1; break; case 'n': - n = atol(optarg); - if (n <= 1) { - fprintf(stderr, "Error: n tiene que ser mayor de 1!\n"); - return 3; + errno = 0; + ninput = strtol(optarg, &check, 10); + if (optarg == check) { + // Empty + print_invalid_n(); + } + else if (errno == ERANGE && ninput == LONG_MIN) { + // Overflow + print_invalid_n(); + } + else if (errno == ERANGE && ninput == LONG_MAX) { + // Underflow + print_invalid_n(); + } + else if (errno == EINVAL) { /* not in all c99 implementations - gcc OK */ + // Base contains unsupported value + // This check is not in all c99 implementations, but does exist in gcc + print_invalid_n(); + } + else if (errno != 0 && ninput == 0) { + // Unspecified error + print_invalid_n(); + } + else if (errno == 0 && optarg && !*check) { + // Valid number + if (ninput > INT_MAX || ninput < INT_MIN) { + fprintf(stderr, "Error: n tiene que ser menor de 2147483648!\n"); + return 6; + } + n = (int) ninput; + if (n <= 1) { + fprintf(stderr, "Error: n tiene que ser mayor de 1!\n"); + return 3; + } + } + else if (errno == 0 && optarg && *check != 0) { + // Contains non number characters + print_invalid_n(); } break; case 'e': @@ -217,15 +291,15 @@ int main (int argc, char **argv) { for (i = 0; i < n; i++) { if (elegir) { opt = 0; - fprintf(stdout, "Elegir elemento %lli: ", i + 1); + fprintf(stdout, "Elegir elemento %d: ", i + 1); while (!read_buffer(&opt)) { - fprintf(stdout, "Número invalido! Tiene que ser mayor de 1!\n"); - fprintf(stdout, "Elegir elemento %lli: ", i + 1); + fprintf(stdout, "Número invalido! Tiene que ser mayor de -2147483649 y menor de 2147483648!\n"); + fprintf(stdout, "Elegir elemento %d: ", i + 1); } unordered_array[i] = opt; } else { - unordered_array[i] = gen_rand((n * 10) * -1, n * 10); + unordered_array[i] = gen_rand(-100000000, 100000000); } }