aboutsummaryrefslogtreecommitdiff
path: root/semestre 2/informatique/tme/semaine4/23_temperature.c
diff options
context:
space:
mode:
Diffstat (limited to 'semestre 2/informatique/tme/semaine4/23_temperature.c')
-rw-r--r--semestre 2/informatique/tme/semaine4/23_temperature.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/semestre 2/informatique/tme/semaine4/23_temperature.c b/semestre 2/informatique/tme/semaine4/23_temperature.c
new file mode 100644
index 0000000..3762987
--- /dev/null
+++ b/semestre 2/informatique/tme/semaine4/23_temperature.c
@@ -0,0 +1,40 @@
+#include <stdlib.h>
+#include <time.h>
+#include <stdio.h>
+
+void init_temp(float tab[31]){
+ for (int i = 0; i < 31; i++)tab[i] = (float) (rand()%500-200)/10;
+}
+
+float moy_temp(float tab[31]){
+ float s = 0;
+ for (int i = 0; i < 31; i++) s += tab[i];
+ return s / 31;
+}
+
+float moy_temp_neg(float tab[31]){
+ float s = 0;
+ int n = 0;
+ for (int i = 0; i < 31; i++) {
+ if (tab[i] < 0){
+ s += tab[i];
+ n++;
+ }
+ }
+ if (n == 0) {
+ return 1;
+ }
+ return s/n;
+}
+
+int main() {
+ srand(time(NULL));
+ float tab[31];
+ init_temp(tab);
+ for (int i = 0; i < 31; i++) printf("%.2f\t", tab[i]);
+ printf("\n%.2f\n", moy_temp(tab));
+ float v = moy_temp_neg(tab);
+ if (v > 0) printf("Aucune température en dessous de zero.");
+ else printf("%.2f", v);
+ return 0;
+}