}
/* number of occurences of an element in an unsorted array */
-int count_tab_element(int tab[], unsigned tab_size, int element) {
+unsigned count_tab_element(int tab[], unsigned tab_size, int element) {
unsigned el_count = 0;
+
for (unsigned i = 0; i < tab_size; i++) {
if (tab[i] == element) {
el_count++;
return el_count;
}
+unsigned count_tab_criteria(int tab[], unsigned tab_size, count_criteria_cb c_criteria) {
+ unsigned cr_count = 0;
+
+ for (unsigned i = 0; i < tab_size; i++) {
+ if (c_criteria(tab[i])) {
+ cr_count++;
+ }
+ }
+ return cr_count;
+}
+
+bool is_even(int a) {
+ return (a % 2 == 0);
+}
+
+bool is_odd(int a) {
+ return (a % 2 != 0);
+
+}
+
void sort_tab(int tab[], unsigned tab_size, criteria_cb criteria) {
sort_bubble_array(tab, tab_size, criteria);
}
#ifndef ARRAY_H
#define ARRAY_H
+#include <stdbool.h>
+
#include "sort.h"
+typedef bool(*count_criteria_cb)(int a);
+
+bool is_even(int a);
+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* resize_tab(int tab[], unsigned tab_size);
-int count_tab_element(int tab[], unsigned tab_size, int element);
+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);
#endif /* ARRAY_H */
printf("2) Trier le tableau.\n");
printf("3) Afficher le tableau.\n");
printf("4) Compter le nombre d'occurence d'un entier dans le tableau.\n");
- printf("5) Quitter.\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");
}
int* do_concat(int array[], unsigned* size) {
criteria = ascending_and_odd;
break;
default:
+ /* sort ascending by default, unused code path */
criteria = ascending;
break;
}
display_choice_menu();
errno = prompt_value("Choix?", &choice);
handle_prompt_error(errno);
- if (1 > choice || 5 < choice) {
- printf("\nFaire un choix compris entre 1 et 5\n");
+ if (1 > choice || 7 < choice) {
+ printf("\nFaire un choix compris entre 1 et 7\n");
continue;
}
switch (choice) {
case 4:
do_count(tab, tab_size);
break;
+ case 5:
+ printf("\nLe nombre d'entiers pairs dans le tableau est %d\n", count_tab_criteria(tab, tab_size, is_even));
+ break;
+ case 6:
+ printf("\nLe nombre d'entiers impairs dans le tableau est %d\n", count_tab_criteria(tab, tab_size, is_odd));
+ break;
default:
/* do nothing, unused code path */
break;
}
- } while (choice != 5);
+ } while (choice != 7);
free_tab(tab);
exit(EXIT_SUCCESS);