aboutsummaryrefslogtreecommitdiff
path: root/semestre 2/informatique/tme/semaine4/23_temperature.c
blob: 3762987a4b87f8c54747fb682f0c31392fd2816d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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;
}