code faux à corriger (dichotomie itérative)

code faux à corriger (dichotomie itérative)

by Jean-Cédric Chappelier -
Number of replies: 0

Voici le code faux à corriger :

#include <iostream>
using namespace std;

int main()
{
  int min(1);
  int max(100);
  
  cout << "Pensez à un nombre entier entre " 
       << min << " et " << max
       << endl;

  bool trouve(false);
  int proposition((min+max)/2);

  do {

    proposition = (min+max) / 2;
    
    cout << "Votre nb est il < > = à " << proposition << " ? ";
    char lu;
    cin >> lu;

    if (lu == '=') {
      trouve = true;
    } else if (lu == '<') {
      max = proposition + 1;
    } else {
      min = proposition - 1;
    }

  } while (not trouve);

  cout << "Votre nombre est : " << proposition << endl;
  
  return 0;
}