I don't have the math knowledge to understand the maxTries thing. I did this exercise from chapter 8 last June, and I'm only starting chapter 13 now. Moving slowly enough already so I only do the minimal viable product.
Code:
#include <stdio.h>
int main(void) {
int guess = 50;
char hilow;
int min = 1, max = 100;
printf("Pick an integer from 1 to 100. I will try to guess ");
printf("it.\nRespond with a y if my guess is right and with");
printf("\nan l if it is low and with an h if it is high.\n");
printf("Uh...is your number %d?\n", guess);
while ((hilow = (char)getchar()) != 'y') { /* get response, compare to y */
if ('h' == hilow) {
max = guess - 1;
guess = min + (max - min) / 2;
} else if ('l' == hilow) {
min = guess + 1;
guess = min + (max - min) / 2;
}
if (hilow != '\n')
printf("Well, then, is it %d?\n", guess);
}
printf("I knew I could do it!\n");
}
It kind of bothers me that I've repeated myself with
Code:
guess = min + (max - min) / 2;