diff --git a/Programmation/Exercices/serie_08_ex2_corr.c b/Programmation/Exercices/serie_08_ex2_corr.c new file mode 100644 index 0000000000000000000000000000000000000000..f8961695fb8088b35da5554ee8453226c1885c99 --- /dev/null +++ b/Programmation/Exercices/serie_08_ex2_corr.c @@ -0,0 +1,104 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> + +void swap(int* a, int* b){ + int tmp = *a; + *a = *b; + *b = tmp; +} + +void fill(int* tab, int size){ + for(int i=0; i<size; i++){ + tab[i] = i; + } +} + +void permut_random(int* tab, int size){ + for(int i=0; i<size; i++){ + swap(tab+i, tab+(rand()%size)); + } +} + +void permut_cyclic(int* tab, int size, int n){ + int* new = malloc(size*sizeof(int)); + for(int i=0; i<size; i++){ + new[(i+n)%size] = tab[i]; + } + for(int i=0; i<size; i++){ + tab[i] = new[i]; + } + free(new); +} + +void place_smaller_at_end(int* tab, int size){ + int ind_min = 0; + for(int i=1; i<size; i++){ + if(tab[i]<tab[ind_min]) ind_min = i; + } + swap(tab+ind_min, tab+size-1); +} + +void print(int* tab, int size){ + for(int i=0; i<size; i++){ + printf("%d, ", tab[i]); + } +} + +void sum(int* tab, int* tab_two, int* tab_three, int size){ + for(int i=0; i<size; i++){ + tab_three[i] = tab[i] + tab_two[i]; + } +} + +void main(){ + + int size; + srand(time(NULL)); + + printf("Entrez la taille du tableau : "); + scanf("%d", &size); + + int* tab = malloc(size*sizeof(int)); + + fill(tab, size); + printf("fill : "); + print(tab, size); + printf("\n"); + + printf("permut random : "); + permut_random(tab, size); + print(tab, size); + printf("\n"); + + printf("permut cyclic 3 : "); + permut_cyclic(tab, size, 3); + print(tab, size); + printf("\n"); + + printf("place smaller at the end: "); + place_smaller_at_end(tab, size); + print(tab, size); + printf("\n"); + + int* tab_two = malloc(size*sizeof(int)); + + fill(tab_two, size); + permut_random(tab_two, size); + permut_cyclic(tab_two, size, 3); + place_smaller_at_end(tab_two, size); + printf("tab_two : "); + print(tab_two, size); + printf("\n"); + + int* tab_three = malloc(size*sizeof(int)); + + sum(tab, tab_two, tab_three, size); + printf("sum : "); + print(tab_three, size); + printf("\n"); + + free(tab); + free(tab_two); + free(tab_three); +} \ No newline at end of file