aboutsummaryrefslogtreecommitdiff
path: root/semestre 2/informatique/tme
diff options
context:
space:
mode:
Diffstat (limited to 'semestre 2/informatique/tme')
-rw-r--r--semestre 2/informatique/tme/semaine3/17_compter_random.c29
-rw-r--r--semestre 2/informatique/tme/semaine3/18_tri_trois_valeurs.c38
-rw-r--r--semestre 2/informatique/tme/semaine3/19_moyenne_min_max.c51
-rw-r--r--semestre 2/informatique/tme/semaine3/20_calcul_racines_polynomes.c73
4 files changed, 191 insertions, 0 deletions
diff --git a/semestre 2/informatique/tme/semaine3/17_compter_random.c b/semestre 2/informatique/tme/semaine3/17_compter_random.c
new file mode 100644
index 0000000..10a1ff3
--- /dev/null
+++ b/semestre 2/informatique/tme/semaine3/17_compter_random.c
@@ -0,0 +1,29 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#define NB_VALEURS 20
+#define VMIN -20
+#define VMAX 20
+
+int valeur_aleatoire(int min, int max){
+ return min + rand()%(max+1-min);
+}
+
+void pos_neg_zero(int *neg, int *zero, int *pos, int v){
+ if (v > 0) (*pos)++;
+ else if (v < 0) (*neg)++;
+ else (*zero)++;
+}
+
+int main(){
+ srand(time(NULL));
+ int neg = 0, pos = 0, zero = 0;
+ for (int i = 0; i < NB_VALEURS; i++){
+ int v = valeur_aleatoire(VMIN, VMAX);
+ pos_neg_zero(&neg, &zero, &pos, v);
+ }
+ printf("valeurs:\n\t- positives: %d\n\t- nulles: %d\n\t- négatives: %d\n", pos, zero, neg);
+ return 0;
+}
+
diff --git a/semestre 2/informatique/tme/semaine3/18_tri_trois_valeurs.c b/semestre 2/informatique/tme/semaine3/18_tri_trois_valeurs.c
new file mode 100644
index 0000000..cfea419
--- /dev/null
+++ b/semestre 2/informatique/tme/semaine3/18_tri_trois_valeurs.c
@@ -0,0 +1,38 @@
+#include <assert.h>
+
+void echange(int *a, int *b){
+ int t = *a;
+ *a = *b;
+ *b = t;
+}
+
+void tri(int *a, int *b){
+ if (*a <= *b) return;
+ echange(a,b);
+}
+
+void tri_3(int *a, int *b, int *c){
+ tri(a, b);
+ tri(a, c);
+ tri(b, c);
+}
+
+int main(){
+ int a = 2, b = 5;
+ echange(&a, &b);
+ assert(a == 5);
+ assert(b == 2);
+ tri(&a, &b);
+ assert(a == 2);
+ assert(b == 5);
+ int c = 8;
+ a = 4; b = 2;
+ tri_3(&a, &b, &c);
+ assert(a == 2 && b == 4 && c == 8);
+ a = 2; b = 4; c = 8;
+ tri_3(&a, &b, &c);
+ assert(a == 2 && b == 4 && c == 8);
+ a = 8; b = 4; c = 2;
+ tri_3(&a, &b, &c);
+ assert(a == 2 && b == 4 && c == 8);
+}
diff --git a/semestre 2/informatique/tme/semaine3/19_moyenne_min_max.c b/semestre 2/informatique/tme/semaine3/19_moyenne_min_max.c
new file mode 100644
index 0000000..11c66db
--- /dev/null
+++ b/semestre 2/informatique/tme/semaine3/19_moyenne_min_max.c
@@ -0,0 +1,51 @@
+#include <stdio.h>
+#include <assert.h>
+#define VAL1 12
+#define VAL2 7
+#define VAL3 5
+#define VAL4 9
+
+void min_max(int *min, int *max, int v){
+ if (v > *max) *max = v;
+ if (v < *min) *min = v;
+}
+
+void stats(float*moy, int*max, int*min, int a, int b, int c, int d){
+ if (a < 0) return;
+ int sum = a;
+ *min = a;
+ *max = a;
+ if (b < 0) {
+ *moy = a;
+ return;
+ }
+ sum += b;
+ min_max(min, max, b);
+ if (c < 0) {
+ *moy = sum/ (float) 2;
+ return;
+ }
+ sum += c;
+ min_max(min, max, c);
+ if (d < 0){
+ *moy = sum/ (float) 3;
+ return;
+ }
+ sum += d;
+ min_max(min, max, d);
+ *moy = sum/ (float) 4;
+}
+
+void afficher_resultat(float moyenne, int min, int max){
+ printf("max = %d, min = %d, moy = %.2f\n",max,min,moyenne);
+}
+
+int main(){
+ float moy = -1;
+ int min, max = -1;
+
+ stats(&moy, &max, &min, VAL1, VAL2, VAL3, VAL4);
+
+ afficher_resultat(moy, min, max);
+ return 0;
+}
diff --git a/semestre 2/informatique/tme/semaine3/20_calcul_racines_polynomes.c b/semestre 2/informatique/tme/semaine3/20_calcul_racines_polynomes.c
new file mode 100644
index 0000000..08a9cdc
--- /dev/null
+++ b/semestre 2/informatique/tme/semaine3/20_calcul_racines_polynomes.c
@@ -0,0 +1,73 @@
+#include <stdio.h>
+#include <math.h>
+
+int nb_racines(int a, int b, int c){
+ float delta = b*b-4*a*c;
+ if (delta > 0) return 2;
+ else if (delta == 0) return 1;
+ return 0;
+}
+
+int nb_racines_delta(int a, int b, int c, float*delta){
+ *delta = b*b-4*a*c;
+ if (*delta > 0) return 2;
+ else if (*delta == 0) return 1;
+ return 0;
+}
+
+int racines(int a, int b, int c, float*r1, float*r2){
+ float delta;
+ int nb = nb_racines_delta(a, b, c, &delta);
+ *r1 = (-b-sqrt(delta))/(2*a);
+ *r2 = (-b+sqrt(delta))/(2*a);
+ return nb;
+}
+
+void affiche_polynome(int a, int b, int c){
+ printf("Polynome : ");
+ if (a == -1) printf("-");
+ else if (a != 1) printf("%d", a);
+ printf("x*x");
+
+ if (b != 0) {
+ printf(" ");
+ if (b == -1) printf("-");
+ else if (b != 1 && b > 0) printf("+%d", b);
+ else if (b != 1 && b < 0) printf("%d", b);
+ printf("x");
+ }
+
+ if (c != 0) {
+ printf(" ");
+ if (c > 0) printf("+%d", c);
+ else if (c < 0) printf("%d", c);
+ }
+ printf("\n");
+}
+
+void affiche_racine(int a, int b, int c){
+ float r1 = 1.0, r2 = 1.0;
+
+ int nb_r = racines(a, b, c, &r1, &r2);
+ if (nb_r == 2) printf("Le polynome a 2 racines : %.3f et %.3f\n", r1, r2);
+ else if (nb_r == 1) printf("Le polynome a une racine double : %.3f\n", r1);
+ else printf("Le polynome n'a pas de racine reelle.\n");
+}
+
+int main(){
+ printf("\n\n=========================================================\n\n");
+ int a = 4, b = 4, c = 1;
+ affiche_polynome(a, b, c);
+ affiche_racine(a, b, c);
+ printf("\n");
+
+ a = 4; b = 6; c = 1;
+ affiche_polynome(a, b, c);
+ affiche_racine(a, b, c);
+ printf("\n");
+
+ a = -7; b = -5; c = -1;
+ affiche_polynome(a, b, c);
+ affiche_racine(a, b, c);
+ return 0;
+}