From f4fcc5493aab8ed30389679fe268cde3142be558 Mon Sep 17 00:00:00 2001 From: "paul.albuquer" <paul.albuquerque@hesge.ch> Date: Sun, 17 Oct 2021 21:26:33 +0200 Subject: [PATCH] added 1st version lesson 3, source codes for lessons --- lessons/contenu_cours_3.md | 138 ++++++++++++++++++ source_codes/refactor_with_functions/a.out | Bin 0 -> 17120 bytes .../anagramme_refactored.c | 72 +++++++++ .../array_1D_init_find_min_refactored.c | 51 +++++++ .../chess_queen_refactored.c | 71 +++++++++ .../chess_queen_refactored_part.c | 67 +++++++++ .../eratosthene_refactored.c | 55 +++++++ .../factorielle_refactored.c | 20 +++ .../nb1er_refactored.c | 30 ++++ .../palindrome_refactored.c | 45 ++++++ .../refactor_with_functions/pgcd_refactored.c | 41 ++++++ .../refactor_with_functions/ppcm_refactored.c | 36 +++++ .../tri_select_refactored.c | 78 ++++++++++ .../tri_select_refactored_part.c | 55 +++++++ .../tri_select_to_refactor.c | 40 +++++ source_codes/tableaux_1d/anagramme.c | 57 ++++++++ .../tableaux_1d/array_1D_init_find_min.c | 36 +++++ source_codes/tableaux_1d/eratosthene.c | 30 ++++ source_codes/tableaux_1d/palindrome.c | 39 +++++ source_codes/tableaux_1d/tri_select.c | 56 +++++++ source_codes/tableaux_1d/tri_select_ebauche.c | 47 ++++++ source_codes/tableaux_2d/chess_queen.c | 71 +++++++++ source_codes/tableaux_2d/chess_queen_part.c | 67 +++++++++ 23 files changed, 1202 insertions(+) create mode 100644 lessons/contenu_cours_3.md create mode 100755 source_codes/refactor_with_functions/a.out create mode 100644 source_codes/refactor_with_functions/anagramme_refactored.c create mode 100644 source_codes/refactor_with_functions/array_1D_init_find_min_refactored.c create mode 100644 source_codes/refactor_with_functions/chess_queen_refactored.c create mode 100644 source_codes/refactor_with_functions/chess_queen_refactored_part.c create mode 100644 source_codes/refactor_with_functions/eratosthene_refactored.c create mode 100644 source_codes/refactor_with_functions/factorielle_refactored.c create mode 100644 source_codes/refactor_with_functions/nb1er_refactored.c create mode 100644 source_codes/refactor_with_functions/palindrome_refactored.c create mode 100644 source_codes/refactor_with_functions/pgcd_refactored.c create mode 100644 source_codes/refactor_with_functions/ppcm_refactored.c create mode 100644 source_codes/refactor_with_functions/tri_select_refactored.c create mode 100644 source_codes/refactor_with_functions/tri_select_refactored_part.c create mode 100644 source_codes/refactor_with_functions/tri_select_to_refactor.c create mode 100644 source_codes/tableaux_1d/anagramme.c create mode 100644 source_codes/tableaux_1d/array_1D_init_find_min.c create mode 100644 source_codes/tableaux_1d/eratosthene.c create mode 100644 source_codes/tableaux_1d/palindrome.c create mode 100644 source_codes/tableaux_1d/tri_select.c create mode 100644 source_codes/tableaux_1d/tri_select_ebauche.c create mode 100644 source_codes/tableaux_2d/chess_queen.c create mode 100644 source_codes/tableaux_2d/chess_queen_part.c diff --git a/lessons/contenu_cours_3.md b/lessons/contenu_cours_3.md new file mode 100644 index 0000000..4e30a48 --- /dev/null +++ b/lessons/contenu_cours_3.md @@ -0,0 +1,138 @@ +# Algorithmes et structures de données 2019-20 + +Contenu du cours 3 du 2.10.2019 + +***** + +## Tableaux à deux dimensions + +- Image noir et blanc avec des booléens : initialisation +```C + int n = 8, m = 8; + bool image_nb[n][m]; + for (int i=0;i<n;i++) { + for (int j=0;j<m;j++) { + image_nb[i][j] = true; + } + } +``` +- Image en niveaux de gris : initialisation +```C + int n = 8, m = 8; + int image_gris[n][m]; + for (int i=0;i<n;i++) { + for (int j=0;j<m;j++) { + image_gris[i][j] = rand()%256; + } + } +``` +> +* Négatif d'une image à 256 niveaux de gris + - Appliquer à chaque pixel l'opération `pixel = 255 - pixel` +```C + for (int i=0;i<n;i++) { + for (int j=0;j<m;j++) { + image_gris[i][j] = 255 - image_gris[i][j]; + } + } + +``` + +## Type énuméré + +- On définit un type énuméré `semaine, coin, mois, couleur, direction` en énumérant toutes ses valeurs possibles. + Explicitement + +```C + enum semaine {lu,ma,me,je,ve,sa,di}; + enum coin {cinq_cts=5,dix_cts=10,vingt_cts=20,euro=100}; + enum mois { + jan=1,feb,mar,apr,mai,jun,jul,aug,sep,oct,nov,dec + }; + enum couleur {RED,GREEN,BLUE}; + enum direction {EST=2,OUEST=3,NORD=0,SUD=1}; +``` + +> Les valeurs d’un type énuméré peuvent être considérées comme des entiers. +Sans précision particulière, le premier élément correspond à 0 et les suivants à 1, 2, etc. +Sinon on peut explicitement spécifier des valeurs. +Rien n’empêche d'attribuer plusieurs fois le même nombre. + +## Opérateur de contrôle `switch` + +- Comparaison entre une valeur et un ensemble de choix pour sélectionner une action à effectuer. Toutes les possibilités doivent être prises en compte. + +```C + void main() { + type data = id1; + int val; + switch(data) { + case ID1: val = data+1; break; + case ID2: val = data*2; break; + case ID3: val = data/3.0; break; + }; + println!(\"val =%d\\n\",val); + } +``` + +## Exemple de la « couverture de la reine » + +```C + enum piece {SAFE,VULN,QUEEN}; + int size = 8; + piece board[size][size]; + // initialisation de l'échiquier + for (int i=0;i<size;i++) { + for (int j=0;j<size;j++) { + board[i][j] = SAFE; + } + } + int pos_reine[] = {3,4}; + // couverture sans les diagonales !!! + for (int k=0;k<size;k++) { + board[k][pos_reine[1]] = VULN; + board[pos_reine[0]][k] = VULN; + } + board[pos_reine[0]][pos_reine[1]] = QUEEN; +``` + +## Matrices + +1. Tableau 2D de nombres + +2. Opérations arithmétiques + 1. Addition : `C = A+B` + 2. Soustraction : `C = A-B` + 3. Multiplication : `C = A*B` (produit lignes/colonnes) + 4. Puissance entière : `C = A`<sup>n</sup> + +## Type article (enregistrement, record) : `struct` + +- Un article permet de regrouper des types de nature différente (appelés champs) comme composant d\'un type composé. + Exemples : + +```C + struct fraction { + int num; + int den; + }; + + enum mois { + jan=1,feb,mar,apr,mai,jun,jul,aug,sep,oct,nov,dec + }; + + struct date { + int day; + mois month; + int year; + } + + enum sexe {FEM, MASC, AUTRE}; + + struct employe { + char nom[80]; + sexe genre; + int age; + float salaire; + } +``` diff --git a/source_codes/refactor_with_functions/a.out b/source_codes/refactor_with_functions/a.out new file mode 100755 index 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L1Ouf*K2+X9V$=qv?nBi(oWNJxDDG!}u_I1DbwV{|QFJ+9e>p zAPmz7qj#d|hqX^&^i(tt!}Nj70%6c9TTr?Lg)fK?8?S-UuyGua90;Qs$8Z#CKP>&A z?>`6SPmn$shQ%L@e-=$YtbYfiVf(CM>e20o^aBv-57rNa(KA4vgJ}ls`$S@cm`@oX zeJWV@9oBE?fbMTccQ1?&qdzh*Fo0GQ!}P=Yl@o~6|Ca%hc47Kq{ZH8bde}aCQ2qnC z7iKRk9#=!d52hd1Pwjv<P%A*1kT6U?jAjG(e?VgiP(`o|Zvf%L?1Av0gHtdLbdDcn z7R-KFzgGaNA0&sQ9HaaagxU{P4p$1(uLT!@FksP%OvBS2L<yL5fEvgF4LDf_1_sd1 z1DJb2WeG?jC$#(pv5~O{sN>AQz_1g|aG1R?wV-Y|%<nKU5Y1=>A{iK@&@`ZN831JH BpdSDL literal 0 HcmV?d00001 diff --git a/source_codes/refactor_with_functions/anagramme_refactored.c b/source_codes/refactor_with_functions/anagramme_refactored.c new file mode 100644 index 0000000..fa4e22b --- /dev/null +++ b/source_codes/refactor_with_functions/anagramme_refactored.c @@ -0,0 +1,72 @@ +#include <stdio.h> +#include <stdbool.h> +#include <string.h> + +bool anagramme_1(char mot1[],char mot2[]); +bool anagramme_2(char mot1[],char mot2[]); +void sort(char mot[]); + +void main() { + char mot1[30]; + char mot2[30]; + printf("Entrez deux mots:\n"); + scanf("%s%s",mot1,mot2); + printf("Les mots %s et %s",mot1,mot2); + + + + if (anagramme_1(mot1,mot2)) { + printf(" sont des anagrammes\n"); + } else { + printf(" ne sont pas des anagrammes\n"); + } +} + +bool anagramme_1(char mot1[],char mot2[]) { + sort(mot1); + sort(mot2); + return (strcmp(mot1,mot2) == 0); +} + +bool anagramme_2(char mot1[],char mot2[]) { + int lg1 = strlen(mot1), lg2 = strlen(mot2); + bool egal = true; + if (lg1 == lg2) { + for (int i=0;i<lg1;i++) { + if (mot1[i] != mot2[i]) { + egal = false; + break; + } + } + } else { + egal = false; + } + return egal; +} + +int index_min(char mot[],int i) { + int ind_min = i; + for (int k=i+1;k<strlen(mot);k++) { + if (mot[k] < mot[ind_min]) { + ind_min = k; + } + } + return ind_min; +} + +void permut(char mot[],int i,int j) { + char tmp = mot[i]; + mot[i] = mot[j]; + mot[j] = tmp; +} + +void sort(char mot[]) { + int lg = strlen(mot); + for (int i=0;i<lg-1;i++) { + int ind_min = index_min(mot,i); + char tmp = mot[i]; + mot[i] = mot[ind_min]; + mot[ind_min] = tmp; + } +} + diff --git a/source_codes/refactor_with_functions/array_1D_init_find_min_refactored.c b/source_codes/refactor_with_functions/array_1D_init_find_min_refactored.c new file mode 100644 index 0000000..57f0241 --- /dev/null +++ b/source_codes/refactor_with_functions/array_1D_init_find_min_refactored.c @@ -0,0 +1,51 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> + +// remplissage d'un tableau avec des nombres aléatoires +void random_init(int n,int tab[n]) { + for (int i=0;i<n;i++) { + tab[i] = rand()%100; + } + printf("\n"); +} + +// remplissage d'un tableau avec le carré de l'index +void square_init(int n,int tab[n]) { + for (int i=0;i<n;i++) { + tab[i] = i*i; + } +} + +// recherche de l'indice de la valeur min dans un tableau +int index_min(int n,int tab[n]) { + int ind_min = 0; + for (int i=1;i<n;i++) { + if (tab[i] < tab[ind_min]) { + ind_min = i; + } + } + return ind_min; +} + +void print(int n,int tab[n]) { + for (int i=0;i<n;i++) { + printf("%d ",tab[i]); + } + printf("\n"); +} + +void main() { + srand(time(NULL)); + int size; + printf("size = "); + scanf("%d",&size); + int tab[size]; + square_init(size,tab); + print(size,tab); + random_init(size,tab); + print(size,tab); + int ind_min = index_min(size,tab); + printf("Tab: index du min = %d / valeur min = %d de tab\n",ind_min,tab[ind_min]); +} + diff --git a/source_codes/refactor_with_functions/chess_queen_refactored.c b/source_codes/refactor_with_functions/chess_queen_refactored.c new file mode 100644 index 0000000..d4da65e --- /dev/null +++ b/source_codes/refactor_with_functions/chess_queen_refactored.c @@ -0,0 +1,71 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef enum _piece {SAFE, VULN, QUEEN} piece; + +void print(int n,int m,piece tab[n][m]); +void init(int n,int m,piece tab[n][m],piece pce); +void couverture(int n,int m,piece tab[n][m],int r_x,int r_y); + + +void main() { + int n=8; + piece board[n][n]; + init(n,n,board,SAFE); + + print(n,n,board); + printf("Entrez la colonne de la reine (1..%d): ",n); + int r_j; + scanf("%d",&r_j); + r_j--; + printf("Entrez la ligne de la reine ('a'..'%c'): ",n-1+'a'); + char ch[1]; + scanf("%s",ch); + couverture(n,n,board,ch[0]-'a',r_j); + print(n,n,board); +} + +void print(int n,int m,piece board[n][m]) { + for (int i=n-1;i>=0;i--) { + printf("%c ",'a'+i); + for (int j=0;j<m;j++) { + switch(board[i][j]) { + case VULN: printf(" *"); break; + case SAFE : printf(" o"); break; + case QUEEN: printf(" R"); + } + } + printf("\n"); + } + printf("\n "); + for (int j=0;j<m;j++) { + printf(" %d",j+1); + } + printf("\n"); +} + +void couverture(int n,int m,piece board[n][m],int r_i,int r_j) { + for (int k=0;k<n;k++) { + board[k][r_j] = VULN; //colonne de la reine + } + for (int k=0;k<m;k++) { + board[r_i][k] = VULN; //ligne de la reine + int tmp = r_j-k; + if (0 <= r_i+tmp && r_i+tmp < n) { //diagonale montante + board[r_i+tmp][k] = VULN; + } + if (0 <= r_i-tmp && r_i-tmp < n) { //diagonale descendante + board[r_i-tmp][k] = VULN; + } + } + board[r_i][r_j] = QUEEN; +} + +void init(int n,int m,piece tab[n][m],piece pce) { + for (int i=0;i<n;i++) { + for (int j=0;j<m;j++) { + tab[i][j] = pce; + } + } +} + diff --git a/source_codes/refactor_with_functions/chess_queen_refactored_part.c b/source_codes/refactor_with_functions/chess_queen_refactored_part.c new file mode 100644 index 0000000..d3d3a91 --- /dev/null +++ b/source_codes/refactor_with_functions/chess_queen_refactored_part.c @@ -0,0 +1,67 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef enum _piece {SAFE, VULN, QUEEN} piece; + +void print(int n,int m,cell tab[n][m]); +void init_board(int n,int m,cell tab[n][m]); +void couverture(int n,int m,cell tab[n][m],int r_x,int r_y); + +void main() { + int n=8; + piece board[n][n]; + init(n,n,board,SAFE); + + print(n,n,board); + printf("Entrez la colonne de la reine (1..%d): ",n); + int r_j; + scanf("%d",&r_j); + r_j--; + printf("Entrez la ligne de la reine ('a'..'%c'): ",n-1+'a'); + char ch[1]; + scanf("%s",ch); + couverture(n,n,board,ch[0]-'a',r_j); + print(n,n,board); +} + +void print(int n,int m,piece board[n][m]) { + for (int i=n-1;i>=0;i--) { + printf("%c ",'a'+i); + for (int j=0;j<m;j++) { + switch(board[i][j]) { + case VULN: printf(" *"); break; + case SAFE : printf(" o"); break; + case QUEEN: printf(" R"); + } + } + printf("\n"); + } + printf("\n "); + for (int j=0;j<m;j++) { + printf(" %d",j+1); + } + printf("\n"); +} + +void couverture(int n,int m,piece board[n][m],int r_i,int r_j) { + //colonne de la reine + for (int k=0;k<n;k++) { + board[k][r_j] = VULN; + } + //ligne de la reine + for (int k=0;k<m;k++) { + board[r_i][k] = VULN; + } + //diagonale montante + for (int k=0;r_i+k<n && r_j+k<m;k++) { + board[r_i+k][r_j+k] = VULN; + } + //compléter les autres diagonales + + board[r_i][r_j] = QUEEN; +} + +void init(int n,int m,piece tab[n][m],piece pce) { + // à compléter +} + diff --git a/source_codes/refactor_with_functions/eratosthene_refactored.c b/source_codes/refactor_with_functions/eratosthene_refactored.c new file mode 100644 index 0000000..bb6332d --- /dev/null +++ b/source_codes/refactor_with_functions/eratosthene_refactored.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <stdbool.h> + +void init(int n,bool tab[n],bool val); +void tracer_multiples(int n,bool tab[n],int i); +void garder_nb1er(int n,bool tab[n]); +void print_nb1er(int n,bool tab[n]); + +void main() { + int size; + printf("size = "); + scanf("%d",&size); + bool tab[size]; + init(size,tab,true); + garder_nb1er(size,tab); + printf("Nombres 1er < %d:",size); + print_nb1er(size,tab); +} + +void init(int n,bool tab[n],bool val) { + for (int i=0;i<n;i++) { + tab[i] = true; + } +} + +void tracer_multiples(int n,bool tab[n],int i) { + int j = i; + while (true) { + j += i; + if (j >= n) { + break; + } + if (tab[j]) { + tab[j] = false; + } + } +} + +void garder_nb1er(int n,bool tab[n]) { + for (int i=2;i<n;i++) { + if (tab[i]) { + tracer_multiples(n,tab,i); + } + } +} + +void print_nb1er(int n,bool tab[n]) { + for (int i=2;i<n;i++) { + if (tab[i]) { + printf("%d ",i); + } + } + printf("\n"); +} + diff --git a/source_codes/refactor_with_functions/factorielle_refactored.c b/source_codes/refactor_with_functions/factorielle_refactored.c new file mode 100644 index 0000000..84c153c --- /dev/null +++ b/source_codes/refactor_with_functions/factorielle_refactored.c @@ -0,0 +1,20 @@ +#include <stdio.h> + +// fonction calculant la factorielle d'un nombre +int factorielle(int n); + +void main() { + int nb = 1; + printf("Entrer un nombre: "); + scanf("%d",&nb); + printf("Fact = %d\n",factorielle(nb)); +} + +int factorielle(int n) { + int facto = 1; + for(int i=2;i<=n;i++) { + facto *= i; + } + return facto; +} + diff --git a/source_codes/refactor_with_functions/nb1er_refactored.c b/source_codes/refactor_with_functions/nb1er_refactored.c new file mode 100644 index 0000000..7834fd4 --- /dev/null +++ b/source_codes/refactor_with_functions/nb1er_refactored.c @@ -0,0 +1,30 @@ +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <stdbool.h> + +// fonction testant si un nombre est 1er +bool is_prime(int nb); + +void main() { + int nb = 1; + printf("Entrez un nombre: "); + scanf("%d",&nb); + + if (is_prime(nb)) { + printf("Le nombre %d est premier\n",nb); + } else { + printf("Le nombre %d n'est pas premier\n",nb); + } +} + +bool is_prime(int nb) { + bool premier = true; + for (int div=2;div<=sqrt(nb);div++) { + if (nb%div == 0) { + premier = false; + break; + } + } + return premier; +} diff --git a/source_codes/refactor_with_functions/palindrome_refactored.c b/source_codes/refactor_with_functions/palindrome_refactored.c new file mode 100644 index 0000000..fbfaa3a --- /dev/null +++ b/source_codes/refactor_with_functions/palindrome_refactored.c @@ -0,0 +1,45 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> + +bool is_palindrome_1(char mot[]); +bool is_palindrome_2(char mot[]); + +void main() { + printf("Entrez une chaîne de caractères: "); + char mot[30]; + scanf("%s",mot); + + printf("Le mot %s",mot); + if (is_palindrome_1(mot)) { + printf(" est un palindrome\n"); + } else { + printf(" n'est pas un palindrome\n"); + } +} + +bool is_palindrome_1(char mot[]) { + int lg = strlen(mot); + bool palindrome = true; + int first_idx = 0, last_idx = lg-1; + while (first_idx < last_idx) { + if (mot[first_idx] != mot[last_idx]) { + palindrome = false; + break; + } + first_idx += 1; + last_idx -= 1; + } + return palindrome; +} + +bool is_palindrome_2(char mot[]) { + int lg = strlen(mot); + char inverse[lg+1]; + for (int i=0;i<lg;i++) { + inverse[i] = mot[lg-1+i]; + } + inverse[lg] = '\0'; + return (strcmp(inverse,mot) == 0); +} diff --git a/source_codes/refactor_with_functions/pgcd_refactored.c b/source_codes/refactor_with_functions/pgcd_refactored.c new file mode 100644 index 0000000..31ad8ac --- /dev/null +++ b/source_codes/refactor_with_functions/pgcd_refactored.c @@ -0,0 +1,41 @@ +#include <stdio.h> + +// calcul du PGCD de deux nombres: algorithme naif +int pgcd_naif(int n, int m); +// calcul du PGCD de deux nombres: algorithme d'Euclide +int pgcd_euclide(int n, int m); + +void main() { + int n,m; + printf("n = "); + scanf("%d",&n); + printf("m = "); + scanf("%d",&m); + printf("Le pgcd de %d et %d est %d\n",n,m,pgcd_naif(n,m)); + printf("Le pgcd de %d et %d est %d\n",n,m,pgcd_euclide(n,m)); +} + +// algorithme naif +int pgcd_naif(int n, int m) { + int gcd = 1; + for (int div=n;div>=2;div--) { + if (n%div == 0 && m%div == 0) { + gcd = div; + break; + } + } + return gcd; +} + +// algorithme d'Euclide +int pgcd_euclide(int n, int m) { + int tmp_n = n; + int tmp_m = m; + while (tmp_n%tmp_m > 0) { + int tmp = tmp_n; + tmp_n = tmp_m; + tmp_m = tmp%tmp_m; + } + return tmp_m; +} + diff --git a/source_codes/refactor_with_functions/ppcm_refactored.c b/source_codes/refactor_with_functions/ppcm_refactored.c new file mode 100644 index 0000000..2a3619e --- /dev/null +++ b/source_codes/refactor_with_functions/ppcm_refactored.c @@ -0,0 +1,36 @@ +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <stdbool.h> +// fonctions calculant le plus petit commun multiple (PPCM) de deux nombres +int ppcm_1(int n,int m); +int ppcm_2(int n,int m); + +void main() { + int n,m; + printf("n = "); + scanf("%d",&n); + printf("m = "); + scanf("%d",&m); + printf("Le ppcm de %d et %d est %d\n",n,m,ppcm_1(n,m)); + printf("Le ppcm de %d et %d est %d\n",n,m,ppcm_2(n,m)); +} + +int ppcm_1(int n,int m) { + int res = n*m; + for (int i=2;i<=m;i++) { + if (n*i%m == 0) { + res = n*i; + break; + } + } +} + +int ppcm_2(int n,int m) { + int fact = 1; + while (n*fact%m != 0) { + fact++; + } + return fact*n; +} + diff --git a/source_codes/refactor_with_functions/tri_select_refactored.c b/source_codes/refactor_with_functions/tri_select_refactored.c new file mode 100644 index 0000000..e6edd02 --- /dev/null +++ b/source_codes/refactor_with_functions/tri_select_refactored.c @@ -0,0 +1,78 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <stdbool.h> + +void print(int n,double tab[n]); + +void random_init(int n,double tab[n]); + +void tri_selection(int n,double tab[n]); + +bool croissant(int n,double tab[n]); + +int main() { + srand(time(NULL)); + int n; + printf("n="); + scanf("%d",&n); + + double res[n]; + random_init(n,res); + print(n,res); + tri_selection(n,res); + print(n,res); + + if (!croissant(n,res)) { + return EXIT_FAILURE; + } + printf("sorted\n"); + return EXIT_SUCCESS; +} + +void print(int n,double tab[n]) { + for (int i=0;i<n;i++) { + printf("%.2lf ",tab[i]); + } + printf("\n"); +} + +void random_init(int n,double tab[n]) { + for (int i=0;i<n;i++) { + tab[i] = 100.0*rand()/(double)RAND_MAX; + } +} + +int index_min(int n,double tab[n],int i) { + int ind_min = i; + for (int k=i+1;k<n;k++) { + if (tab[k] < tab[ind_min]) { + ind_min = k; + } + } + return ind_min; +} + +void permut(int n,double tab[n],int i,int j) { + double tmp = tab[i]; + tab[i] = tab[j]; + tab[j] = tmp; +} + +void tri_selection(int n,double tab[n]) { + for (int i=0;i<n-1;i++) { + int ind_min = index_min(n,tab,i); + if (tab[i] != tab[ind_min]) { + permut(n,tab,i,ind_min); + } + } +} + +bool croissant(int n,double tab[n]) { + for (int i=0;i<n-1;i++) { + if (tab[i] > tab[i + 1]) { + return false; + } + } + return true; +} diff --git a/source_codes/refactor_with_functions/tri_select_refactored_part.c b/source_codes/refactor_with_functions/tri_select_refactored_part.c new file mode 100644 index 0000000..9ad9082 --- /dev/null +++ b/source_codes/refactor_with_functions/tri_select_refactored_part.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <stdbool.h> + +void print(int n,double tab[n]) { + //à compléter +} + +void random_tab(int n,double tab[n]) { + //à compléter +} + +int index_min(int n,double tab[n],int i) { + int ind_min = i; + //à compléter + return ind_min; +} + +void permut(int n,double tab[n],int i,int j) { + //à compléter +} + +void tri_selection(int n,double tab[n]) { + for (int i=0;i<n-1;i++) { + int ind_min = index_min(n,tab,i); + if (tab[i] != tab[ind_min]) { + permut(n,tab,i,ind_min); + } + } +} + +bool croissant(int n,double tab[n]) { + //à compléter + return true; +} + +int main() { + srand(time(NULL)); + int n; + printf("n="); + scanf("%d",&n); + + double res[n]; + random_tab(n,res); + print(n,res); + tri_selection(n,res); + print(n,res); + + if (!croissant(n,res)) { + return EXIT_FAILURE; + } + printf("sorted\n"); + return EXIT_SUCCESS; +} diff --git a/source_codes/refactor_with_functions/tri_select_to_refactor.c b/source_codes/refactor_with_functions/tri_select_to_refactor.c new file mode 100644 index 0000000..49916b1 --- /dev/null +++ b/source_codes/refactor_with_functions/tri_select_to_refactor.c @@ -0,0 +1,40 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> + +#define SIZE 10 + +int main() { + srand(time(NULL)); + double tab[SIZE]; + for (int i = 0; i < SIZE; ++i) { + tab[i] = rand() / (double)RAND_MAX; + } + + for (int i = 0; i < SIZE - 1; ++i) { + double min = tab[i]; + int ind_min = i; + for (int j = i + 1; j < SIZE; ++j) { + if (min > tab[j]) { + ind_min = j; + min = tab[j]; + } + } + double tmp = tab[i]; + tab[i] = tab[ind_min]; + tab[ind_min] = tmp; + } + + for (int i = 0; i < SIZE; ++i) { + printf("%f ", tab[i]); + } + printf("\n"); + + for (int i = 0; i < SIZE - 1; ++i) { + if (tab[i] > tab[i + 1]) { + return EXIT_FAILURE; + } + } + + return EXIT_SUCCESS; +} diff --git a/source_codes/tableaux_1d/anagramme.c b/source_codes/tableaux_1d/anagramme.c new file mode 100644 index 0000000..d81505e --- /dev/null +++ b/source_codes/tableaux_1d/anagramme.c @@ -0,0 +1,57 @@ +#include <stdio.h> +#include <stdbool.h> +#include <string.h> +void main() { + char mot1[30]; + char mot2[30]; + printf("Entrez deux mots:\n"); + scanf("%s%s",mot1,mot2); + int lg1 = strlen(mot1), lg2 = strlen(mot2); + printf("Les mots %s et %s",mot1,mot2); + + // tri de mot1 + for (int i=0;i<lg1-1;i++) { + int ind_min = i; + for (int k=i+1;k<lg1;k++) { + if (mot1[k] < mot1[ind_min]) { + ind_min = k; + } + } + int tmp = mot1[i]; + mot1[i] = mot1[ind_min]; + mot1[ind_min] = tmp; + } + + // tri de mot2 + for (int i=0;i<lg2;i++) { + int ind_min = i; + for (int k=i+1;k<lg2;k++) { + if (mot2[k] < mot2[ind_min]) { + ind_min = k; + } + } + int tmp = mot2[i]; + mot2[i] = mot2[ind_min]; + mot2[ind_min] = tmp; + } + + /*bool egal = true; + if (lg1 == lg2) { + for (int i=0;i<lg1;i++) { + if (mot1[i] != mot2[i]) { + egal = false; + break; + } + } + } else { + egal = false; + }*/ + bool egal = (strcmp(mot1,mot2) == 0); + + if (egal) { + printf(" sont des anagrammes\n"); + } else { + printf(" ne sont pas des anagrammes\n"); + } +} + diff --git a/source_codes/tableaux_1d/array_1D_init_find_min.c b/source_codes/tableaux_1d/array_1D_init_find_min.c new file mode 100644 index 0000000..c8bc98c --- /dev/null +++ b/source_codes/tableaux_1d/array_1D_init_find_min.c @@ -0,0 +1,36 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +const unsigned int SIZE = 13; + +void main() { + int tab[SIZE]; + for (int i=0;i<SIZE;i++) { + tab[i] = i*i; + } + for (int i=0;i<SIZE;i++){ + printf("%d ",tab[i]); + } + printf("\n"); + + // remplissage d'un tableau avec des nombres aléatoires + srand(time(NULL)); + for (int i=0;i<SIZE;i++) { + tab[i] = rand()%100; + } + for (int i=0;i<SIZE;i++) { + printf("%d ",tab[i]); + } + printf("\n"); + + // recherche de l'indice de la valeur min dans un tableau + int val_min = tab[0],ind_min = 0; + for (int i=0;i<SIZE;i++) { + if (tab[i] < val_min) { + ind_min = i; + val_min = tab[i]; + } + } + printf("Tab: index du min = %d / valeur min = %d de tab\n",ind_min,tab[ind_min]); +} + diff --git a/source_codes/tableaux_1d/eratosthene.c b/source_codes/tableaux_1d/eratosthene.c new file mode 100644 index 0000000..27355be --- /dev/null +++ b/source_codes/tableaux_1d/eratosthene.c @@ -0,0 +1,30 @@ +#include <stdio.h> +#include <stdbool.h> +const int SIZE = 51; + +void main() { + bool tab[SIZE]; + for (int i=0;i<SIZE;i++) { + tab[i] = true; + } + for (int i=2;i<SIZE;i++) { + if (tab[i]) { + int j = i; + while (true) { + j += i; + if (j >= SIZE) { + break; + } + tab[j] = false; + } + } + } + + for (int i=2;i<SIZE;i++) { + if (tab[i]) { + printf("%d ",i); + } + } + printf("\n"); +} + diff --git a/source_codes/tableaux_1d/palindrome.c b/source_codes/tableaux_1d/palindrome.c new file mode 100644 index 0000000..7c00e52 --- /dev/null +++ b/source_codes/tableaux_1d/palindrome.c @@ -0,0 +1,39 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> + +void main() { + printf("Entrez une chaîne de caractères: "); + char mot[30]; + scanf("%s",mot); + int lg = strlen(mot); + + bool palindrome = true; + int first_idx = 0; + int last_idx = lg-1; + while (first_idx < last_idx) { + if (mot[first_idx] != mot[last_idx]) { + palindrome = false; + break; + } + first_idx += 1; + last_idx -= 1; + } + + //alternative + char inverse[30]; + for (int i=0;i<lg;i++) { + inverse[i] = mot[lg-1-i]; + } + inverse[lg] = '\0'; + bool palindrome_alt = (strcmp(inverse,mot) == 0); + + printf("Le mot %s",mot); + if (palindrome_alt) { + printf(" est un palindrome\n"); + } else { + printf(" n'est pas un palindrome\n"); + } +} + diff --git a/source_codes/tableaux_1d/tri_select.c b/source_codes/tableaux_1d/tri_select.c new file mode 100644 index 0000000..18f5f96 --- /dev/null +++ b/source_codes/tableaux_1d/tri_select.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +const int SIZE = 13; + +// Tri par sélection +void main() { + srand(time(NULL)); + float tab[SIZE]; + // initialisation du tableau avec des valeurs aléatoires + for (int i=0;i<SIZE;i++) { + tab[i] = 100.0*(rand()/(float)RAND_MAX); + } + // impression du tableau + for (int i=0;i<SIZE;i++) { + printf("%.2f ",tab[i]); + } + printf("\n"); + + // recherche dans le tableau de l'indice de la plus petite valeur + int ind_min = 0; + for (int k=1;k<SIZE;k++) { + if (tab[k] < tab[ind_min]) { + ind_min = k; + } + } + printf("index du min de tab: %d / valeur min de tab: %.2f\n",ind_min,tab[ind_min]); + + // échange de la 1ère valeur avec la plus petite du tableau + float tmp = tab[0]; + tab[0] = tab[ind_min]; + tab[ind_min] = tmp; + // impression du tableau + for (int i=0;i<SIZE;i++) { + printf("%.2f ",tab[i]); + } + printf("\n"); + // tri par sélection + for (int i=0;i<SIZE-1;i++) { + ind_min = i; + for (int k=i+1;k<SIZE;k++) { + if (tab[k] < tab[ind_min]) { + ind_min = k; + } + } + tmp = tab[i]; + tab[i] = tab[ind_min]; + tab[ind_min] = tmp; + } + // impression du tableau + for (int i=0;i<SIZE;i++) { + printf("%.2f ",tab[i]); + } + printf("\n"); +} + diff --git a/source_codes/tableaux_1d/tri_select_ebauche.c b/source_codes/tableaux_1d/tri_select_ebauche.c new file mode 100644 index 0000000..2ada6c5 --- /dev/null +++ b/source_codes/tableaux_1d/tri_select_ebauche.c @@ -0,0 +1,47 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +const int SIZE = 13; + +// Tri par sélection +void main() { + //déclaration et intialisation du tableau + srand(time(NULL)); + double tab[SIZE]; + for (int i=0;i<SIZE;i++) { + tab[i] = 100.0*(rand()/(double)RAND_MAX); + } + + //impression du tableau + for (int i=0;i<SIZE;i++) { + printf("%.2lf ",tab[i]); + } + printf("\n"); + //à compléter + int debut = 0; + for (....) { //à compléter + // recherche du plus petit élément du sous-tableau de début à SIZE + int ind_min = 0; // à remplacer 0 par debut + double val_min = tab[0]; // à remplacer 0 par debut + for (int i = 1; i < SIZE; ++i) { //à remplacer 1 par debut+1 + if (val_min > tab[i]) { + val_min = tab[i]; + ind_min = i; + } + } + printf("val. min. = %.2lf / index min. = %d\n ",val_min,ind_min); + // permuter le plus petit élément avec le premier élément du tableau + double tmp = tab[0]; // à remplacer 0 par debut + tab[0] = tab[ind_min]; // à remplacer 0 par debut + tab[ind_min] = tmp; + } + + //impression du tableau + for (int i=0;i<SIZE;i++) { + printf("%.2lf ",tab[i]); + } + printf("\n"); + + //validation que le tableau est trié + //à compléter +} diff --git a/source_codes/tableaux_2d/chess_queen.c b/source_codes/tableaux_2d/chess_queen.c new file mode 100644 index 0000000..60a7cb5 --- /dev/null +++ b/source_codes/tableaux_2d/chess_queen.c @@ -0,0 +1,71 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef enum _piece {SAFE, VULN, QUEEN} piece; + +void print(int n,int m,cell tab[n][m]); +void init_board(int n,int m,cell tab[n][m]); +void couverture(int n,int m,cell tab[n][m],int r_x,int r_y); + + +void main() { + int n=8; + piece board[n][n]; + init(n,n,board,SAFE); + + print(n,n,board); + printf("Entrez la colonne de la reine (1..%d): ",n); + int r_j; + scanf("%d",&r_j); + r_j--; + printf("Entrez la ligne de la reine ('a'..'%c'): ",n-1+'a'); + char ch[1]; + scanf("%s",ch); + couverture(n,n,board,ch[0]-'a',r_j); + print(n,n,board); +} + +void print(int n,int m,piece board[n][m]) { + for (int i=n-1;i>=0;i--) { + printf("%c ",'a'+i); + for (int j=0;j<m;j++) { + switch(board[i][j]) { + case VULN: printf(" *"); break; + case SAFE : printf(" o"); break; + case QUEEN: printf(" R"); + } + } + printf("\n"); + } + printf("\n "); + for (int j=0;j<m;j++) { + printf(" %d",j+1); + } + printf("\n"); +} + +void couverture(int n,int m,piece board[n][m],int r_i,int r_j) { + for (int k=0;k<n;k++) { + board[k][r_j] = VULN; //colonne de la reine + } + for (int k=0;k<m;k++) { + board[r_i][k] = VULN; //ligne de la reine + int tmp = r_j-k; + if (0 <= r_i+tmp && r_i+tmp < n) { //diagonale montante + board[r_i+tmp][k] = VULN; + } + if (0 <= r_i-tmp && r_i-tmp < n) { //diagonale descendante + board[r_i-tmp][k] = VULN; + } + } + board[r_i][r_j] = QUEEN; +} + +void init(int n,int m,piece tab[n][m],piece pce) { + for (int i=0;i<n;i++) { + for (int j=0;j<m;j++) { + tab[i][j] = pce; + } + } +} + diff --git a/source_codes/tableaux_2d/chess_queen_part.c b/source_codes/tableaux_2d/chess_queen_part.c new file mode 100644 index 0000000..d3d3a91 --- /dev/null +++ b/source_codes/tableaux_2d/chess_queen_part.c @@ -0,0 +1,67 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef enum _piece {SAFE, VULN, QUEEN} piece; + +void print(int n,int m,cell tab[n][m]); +void init_board(int n,int m,cell tab[n][m]); +void couverture(int n,int m,cell tab[n][m],int r_x,int r_y); + +void main() { + int n=8; + piece board[n][n]; + init(n,n,board,SAFE); + + print(n,n,board); + printf("Entrez la colonne de la reine (1..%d): ",n); + int r_j; + scanf("%d",&r_j); + r_j--; + printf("Entrez la ligne de la reine ('a'..'%c'): ",n-1+'a'); + char ch[1]; + scanf("%s",ch); + couverture(n,n,board,ch[0]-'a',r_j); + print(n,n,board); +} + +void print(int n,int m,piece board[n][m]) { + for (int i=n-1;i>=0;i--) { + printf("%c ",'a'+i); + for (int j=0;j<m;j++) { + switch(board[i][j]) { + case VULN: printf(" *"); break; + case SAFE : printf(" o"); break; + case QUEEN: printf(" R"); + } + } + printf("\n"); + } + printf("\n "); + for (int j=0;j<m;j++) { + printf(" %d",j+1); + } + printf("\n"); +} + +void couverture(int n,int m,piece board[n][m],int r_i,int r_j) { + //colonne de la reine + for (int k=0;k<n;k++) { + board[k][r_j] = VULN; + } + //ligne de la reine + for (int k=0;k<m;k++) { + board[r_i][k] = VULN; + } + //diagonale montante + for (int k=0;r_i+k<n && r_j+k<m;k++) { + board[r_i+k][r_j+k] = VULN; + } + //compléter les autres diagonales + + board[r_i][r_j] = QUEEN; +} + +void init(int n,int m,piece tab[n][m],piece pce) { + // à compléter +} + -- GitLab