Exercice langage C: For, if et booléens

Travail à Faire:

Compléter le programme suivant aux endroits indiqués par // ... pour que les deux affichages apres la boucle for soient corrects. Exécutez plusieurs fois le programme pour tirer des nombres aléatoires différents et tester les différents cas.

Comment écrire le programme sans utiliser de if dans la boucle for ?

#include
#include
#include
using namespace std;
int main(int argc, char **argv) {
bool au_moins_un_nombre_superieur_a_90;
bool aucun_nombre_inferieur_a_10;
srand(time(0));
// ...
for(int i = 0; i int r = rand() % 100;
cout // ...
}
cout if (au_moins_un_nombre_superieur_a_90)
cout if (aucun_nombre_inferieur_a_10)
cout }

#include
using namespace std;
int main(int argc, char **argv) {
bool au_moins_un_nombre_superieur_a_90;
bool aucun_nombre_inferieur_a_10;
srand(time(0));
au_moins_un_nombre_superieur_a_90 = false;
aucun_nombre_inferieur_a_10 = true;
for(int i = 0; i int r = rand() % 100;
cout // version avec des if:
if (r > 90)
au_moins_un_nombre_superieur_a_90 = true;
if (r aucun_nombre_inferieur_a_10 = false;
// version sans les if:
au_moins_un_nombre_superieur_a_90 = au_moins_un_nombre_superieur_a_90 || (r > 90);
aucun_nombre_inferieur_a_10 = aucun_nombre_inferieur_a_10 && (r >= 10);
}
cout if (au_moins_un_nombre_superieur_a_90)
cout if (aucun_nombre_inferieur_a_10)
cout }

Article publié le 17 Mars 2012 Mise à jour le Lundi, 07 Novembre 2022 10:57 par BENKIRANE Fatima Ezzahra