aboutsummaryrefslogtreecommitdiff
path: root/semestre 2/informatique/tme/semaine3/17_compter_random.c
diff options
context:
space:
mode:
Diffstat (limited to 'semestre 2/informatique/tme/semaine3/17_compter_random.c')
-rw-r--r--semestre 2/informatique/tme/semaine3/17_compter_random.c29
1 files changed, 29 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;
+}
+