TP 13 exo1: Implement array resizing
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 14 Mar 2017 22:42:27 +0000 (23:42 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 14 Mar 2017 22:42:27 +0000 (23:42 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
TP_13/exo1/lib/array.c
TP_13/exo1/lib/array.h
TP_13/exo1/lib/io.c
TP_13/exo1/lib/io.h
TP_13/exo1/src/main.c

index 7743ca5132345894f8423c7287bb522411537b8d..57c15def73cb8cebe2e6bbea5ec6d0bb2fc12a08 100644 (file)
@@ -41,8 +41,13 @@ int* concat_tab(int tab1[], unsigned tab_size1, int tab2[], unsigned tab_size2)
     return tab_dest;
 }
 
     return tab_dest;
 }
 
-int* resize_tab(int tab[], unsigned new_tab_size) {
+int* resize_tab(int tab[], unsigned old_tab_size, unsigned new_tab_size) {
     tab = realloc(tab, sizeof(int) * new_tab_size);
     tab = realloc(tab, sizeof(int) * new_tab_size);
+    if (old_tab_size < new_tab_size) {
+        for (unsigned i = old_tab_size; i < new_tab_size; i++) {
+            tab[i] = 0;
+        }
+    }
     return tab;
 }
 
     return tab;
 }
 
index ebf92c525a78f4a5178f3bd1ac6bb3f876d270ab..216fa0aa0b3f2b01e2c7b9ec2e8a0129f90a4dd0 100644 (file)
@@ -13,7 +13,7 @@ bool is_odd(int a);
 int* create_tab(int tab[], unsigned tab_size);
 void free_tab(int tab[]);
 int* concat_tab(int tab1[], unsigned tab_size1, int tab2[], unsigned tab_size2);
 int* create_tab(int tab[], unsigned tab_size);
 void free_tab(int tab[]);
 int* concat_tab(int tab1[], unsigned tab_size1, int tab2[], unsigned tab_size2);
-int* resize_tab(int tab[], unsigned tab_size);
+int* resize_tab(int tab[], unsigned old_tab_size, unsigned new_tab_size);
 unsigned count_tab_element(int tab[], unsigned tab_size, int element);
 unsigned count_tab_criteria(int tab[], unsigned tab_size, count_criteria_cb c_criteria);
 void sort_tab(int tab[], unsigned tab_size, criteria_cb criteria);
 unsigned count_tab_element(int tab[], unsigned tab_size, int element);
 unsigned count_tab_criteria(int tab[], unsigned tab_size, count_criteria_cb c_criteria);
 void sort_tab(int tab[], unsigned tab_size, criteria_cb criteria);
index 7639410116edf56925481f8501b24fc0ff29c963..884d4a0b730f1d6e266628f326866a4e829112a2 100644 (file)
@@ -30,7 +30,8 @@ void display_choice_menu() {
     printf("4) Compter le nombre d'occurence d'un entier dans le tableau.\n");
     printf("5) Compter le nombre d'entiers pairs dans le tableau.\n");
     printf("6) Compter le nombre d'entiers impairs dans le tableau.\n");
     printf("4) Compter le nombre d'occurence d'un entier dans le tableau.\n");
     printf("5) Compter le nombre d'entiers pairs dans le tableau.\n");
     printf("6) Compter le nombre d'entiers impairs dans le tableau.\n");
-    printf("7) Quitter.\n");
+    printf("7) Redimensionner le tableau.\n");
+    printf("8) Quitter.\n");
 }
 
 int* do_concat(int array[], unsigned* size) {
 }
 
 int* do_concat(int array[], unsigned* size) {
@@ -95,6 +96,16 @@ void do_count(int array[], unsigned size) {
     printf("La valeur %d est presente %d fois dans le tableau\n", search_value, count_tab_element(array, size, search_value));
 }
 
     printf("La valeur %d est presente %d fois dans le tableau\n", search_value, count_tab_element(array, size, search_value));
 }
 
+void do_resize(int array[], unsigned* old_size) {
+    int errno = 0;
+    unsigned new_size = 0;
+
+    errno = prompt_value("\nNouvelle taille?", (int*)&new_size);
+    handle_prompt_error(errno);
+    array = resize_tab(array, *old_size, new_size);
+    *old_size = new_size;
+}
+
 void display_array(int array[], unsigned size) {
     if (array != NULL) {
         printf("\n--array begin--\n");
 void display_array(int array[], unsigned size) {
     if (array != NULL) {
         printf("\n--array begin--\n");
index 13f14e0d80b83f997ee9c37a572e6f07679c7492..24de1b1613de17b5f65833080932824dc423de4f 100644 (file)
@@ -9,6 +9,7 @@ void display_choice_menu();
 int* do_concat(int array[], unsigned* size);
 void do_sort(int array[], unsigned size);
 void do_count(int array[], unsigned size);
 int* do_concat(int array[], unsigned* size);
 void do_sort(int array[], unsigned size);
 void do_count(int array[], unsigned size);
+void do_resize(int array[], unsigned* size);
 
 void display_array(int array[], unsigned size);
 
 
 void display_array(int array[], unsigned size);
 
index 2c81a631b28d3abb19c28c4eb6bf4d0ce3e0bd7f..ef4d5455c32177aba07a6eff8f7af458d9931fd1 100644 (file)
@@ -18,8 +18,8 @@ int main() {
         display_choice_menu();
         errno = prompt_value("Choix?", &choice);
         handle_prompt_error(errno);
         display_choice_menu();
         errno = prompt_value("Choix?", &choice);
         handle_prompt_error(errno);
-        if (1 > choice || 7 < choice) {
-            printf("\nFaire un choix compris entre 1 et 7\n");
+        if (1 > choice || 8 < choice) {
+            printf("\nFaire un choix compris entre 1 et 8\n");
             continue;
         }
         switch (choice) {
             continue;
         }
         switch (choice) {
@@ -41,11 +41,14 @@ int main() {
             case 6:
                 printf("\nLe nombre d'entiers impairs dans le tableau est %d\n", count_tab_criteria(tab, tab_size, is_odd));
                 break;
             case 6:
                 printf("\nLe nombre d'entiers impairs dans le tableau est %d\n", count_tab_criteria(tab, tab_size, is_odd));
                 break;
+            case 7:
+                do_resize(tab, &tab_size);
+                break;
             default:
                 /* do nothing, unused code path */
                 break;
         }
             default:
                 /* do nothing, unused code path */
                 break;
         }
-    } while (choice != 7);
+    } while (choice != 8);
 
     free_tab(tab);
     exit(EXIT_SUCCESS);
 
     free_tab(tab);
     exit(EXIT_SUCCESS);