Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think that there is still an issue in your code (even if the bugs signaled before are fixed): it should be clear that <strong>upperbound and lowerbound are both included</strong> in possible numbers remaining and this is not clear from your code: at the first question, if I answer 'y', the new interval is [0,50] and if I answer 'n', the new interval is [50,100], which is wrong! 50 should not be included in the second interval.</p> <p>To fix this, you should change the update of your bounds to something like:</p> <pre><code>if ( userInput == "y" || userInput == "Y" ) upperBound = lowerBound + halfRange; else if ( userInput == "n" || userInput == "n" ) lowerBound = lowerBound + halfRange + 1; </code></pre> <p>Now there is still a problem with the last question, which remains the same forever. The problem is that when range=1, you have halfrange=1 as well and the question keeps the same.</p> <p>To solve that issue, you should round <strong>down</strong> range. Just define it like:</p> <pre><code>int halfRange = ( upperBound - lowerBound ) / 2; </code></pre> <p>and now your code should work. Here is the code I would use for the while loop:</p> <pre><code>while ( lowerBound != upperBound ){ // Increment count ++count; int halfRange = ( upperBound - lowerBound ) / 2; int midpoint = lowerBound + halfRange; cout &lt;&lt; count &lt;&lt;": Is your number between " &lt;&lt; lowerBound &lt;&lt; " and " &lt;&lt; midpoint &lt;&lt; "? (both included) "; cin &gt;&gt; userInput; // Reset the bounds if ( userInput == "y" || userInput == "Y" ) upperBound = midpoint; else if ( userInput == "n" || userInput == "n" ) lowerBound = midpoint + 1; else { --count; cout &lt;&lt; "Error! Answer could not be understood."; } </code></pre> <p>}</p> <p>Remark that now we only use integers, no double at all! If you use integers, try to avoid passing by double, and use rather integer division and modulo to work with them (and do not use double to represent integers).</p> <p>Hope it helps!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload