From d8e509a1aa4f4f8b75eb1a9c14d498966422fae4 Mon Sep 17 00:00:00 2001 From: Geoffrey Frogeye Date: Wed, 9 Mar 2016 08:50:12 +0100 Subject: [PATCH] TP3 E5.c --- TP3/E5.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 TP3/E5.c diff --git a/TP3/E5.c b/TP3/E5.c new file mode 100644 index 0000000..1fa67f0 --- /dev/null +++ b/TP3/E5.c @@ -0,0 +1,48 @@ +/* Maximum et position */ + +#include + +double maximum(int n, double T[n]) { + // Même si n est inferieur à la dimension du tableau, ça marche quand même + int i; + double max = T[0]; + for (i = 0; i < n; i++) { + if (T[i] > max) { + max = T[i]; + } + } + return max; +} +int maximumIndice(int n, double T[n]) { + // Même si n est inferieur à la dimension du tableau, ça marche quand même + int i, maxIndice = 0; + double max = T[0]; + for (i = n-1; i > 0; i--) { + if (T[i] > max) { + max = T[i]; + maxIndice = i; + } + } + return maxIndice; +} + +int main() { + int n, i, t; + printf("Nombre de nombres ? "); + scanf("%d", &n); + double T[n]; + for (i = 0; i < n; i++) { + printf("Saisir le nombre %d : ", i+1); + scanf("%lf", &T[i]); + } + + printf("Sur combien de valeurs travailler ? "); + scanf("%d", &t); + if (t > n) { + printf("Le nombre de valeurs sur lesquelles travailler doit être inférieur à la taille du tableau\n"); + return 2; + } + + printf("Maximum : %lf (à l'indice %d)\n", maximum(t, T), maximumIndice(t, T)+1); + return 0; +}