Min Max variable setup

Towards the end of the lecture we see while debugging the program for highest guess we noticed that the program guessed as far as 999… remember that?

So the fix for that was to add +1 to max variable (max = max +1) so that when the Player picks “1000” as a guess the program can process that number instead of max 999…

Alternatively I set " int max = 1001 "
Reason being computers start counting from “0”, that’s why originally the program only guessed as far as 999, when we set the variable max = 1000.

I had previously learned this when working with LOOPS in JS & C.

Happy Coding! :blush:
Dave

3 Likes

Something I noticed, and to be honest you have to lie to the computer to get this result but it will also not go to a max of 500 once the upper limit is brought down by saying it is lower than 500.

I actually didn’t have that problem, because instead of “min = guess”, I used “min = guess + 1”, and likewise “max = guess - 1”. The downside is if a user mistakenly hits up or down when the guess is correct, it’s possible it never returns to the correct number again.

2 Likes

Hi David. Loving this so far.
I’ve been clearing the console each time I make a change so I can see how edits change the behavior in total. I noticed that when I apply the fix for the 1000 issue - that is, adding “max = max +1;” into the start function, the print line “The highest number you can pick is” changed the option from 1000 to 1001. That makes total sense to me since the whole line reads "print (“The highest number you can pick is " +max);” and we just changed what ‘max’ is. It looks like the same thing would happen if I changed max to “int max = 1001”.

I feel like I’m missing something really obvious but I’m a wee bit stumped. Is the only option to change the print line back to text rather than calling on max? (I’m sure it’s not the only option, but I mean the best option for a total beginner)

Thanks!

Never Mind. Just listened to the lesson where you address this.
Thx!

So I was curious how would one add a question mark to the string on

print ("Is the number high or lower than " +guess);

1 Like

print ("Is the number high or lower than " + guess + “?” );

1 Like

What I found to be the easiest solution is to put in void StartGame ()

max = 1001;
min = 1;
guess = 500;

print(“The highest number you can pick is 1000”);
print(“The lowest number you can pick is 1”);
print(“Is the number higher or lower than 500?”);

That way there is no way for it to mess up the numbers in the instructions, it always goes up to 1000 and down to 1 and doesn’t have any issues when restarting.

1 Like

…but does mean if you want to change the range at any point you will now need to make changes in more than one place. By having the variables displayed with the concatenated text you only need to make the changes in the one place. Just for info etc :slight_smile:

I’m sorry but this is just not correct. First off this issue has nothing at all to do with “how computers count”. The issue is in fact actually as Todd_Vance stated. The algorithm is incorrect in the lesson, and in fact the min and max should be set to guess +1 or guess -1.

Think about what the input is. If you choose number 600, and the computer asks if you chose 500, you are telling the computer no my number is higher than 500. Not higher than or equal to, because if it was equal to then the computer won. No, it is strictly higher than. This is why we should be setting min and max to +1 or -1 as appropriate.

That said, even if you do this there is still one edge case in that the computer will bottom out at guessing 0 instead of 1. This can be solved with a simple bounds check:

		max = guess - 1;
		if (max <= 1)
		{
			max = 1;
		}

When you say talk about computers counting you are referring to how data structures like Lists or Arrays are indexed and in most languages they are indexed starting at 0. Not all though. Some languages do in fact start at 1. Regardless this has nothing to do with this issue, so please understand that so you don’t further confuse yourself.

1 Like

Thank you for the clarification. I was not confusing myself. I have very little knowledge in programming. I was referring to that counting while using loops not lists or arrays. I share what I know as little it may be. Thanks again.

If you are going to use max = max + 1, then it would be much better to put the
max = max +1

at the bottom of your StartGame() function, after the prompt has been printed but before the game starts doing any calculations.

Otherwise your program becomes unnecessarily harder to modify if you ever wanted to change max guess to 2000.

However, as noted modifying the guesses by +/- 1 seems the correct way to do it.

edit: I just now got to the lesson where Ben moves max = max + 1 to the bottom of hte function. Note to self: watch to the end of the lessons in a section before commenting!

1 Like

I’m not sure I understand how typing 2 0 0 1 and 2 0 0 0 is unnecessarily harder. It doesn’t take any effort at all to modify things if done the way I did it. And seeing as my way gets the job done as well, it would seem that there isn’t a “correct” way to do it.

In larger projects with many functions, especially ones where there are multiple developers, it becomes more important.
It’s more about keeping the places where you need to make changes to a minimum. A best practice for ease of maintenance. Hard coding a value when it’s represented by a variable elsewhere is pretty much always a bad idea.

You’re right, though. There’s not a "correct’ way as long as you get the end result you want. One can’t argue with results.

1 Like

In larger projects yes, there are certain ways to do things so that it’s easier to change variables, but we aren’t talking about projects like that. We are talking about a very small project and the issues experienced while working on it so I would hope that any posts/replies would have to do with the project that is being worked on/issue being experienced and not anything else. :slight_smile:

And it does.

I agree with you that there is no “correct” way to do things, as long as desired results are achieved. However, that doesn’t mean that one way isn’t more efficient (and/or more maintainable) than another. Since this is a learning exercise, thinking about these things and developing good habits is relevant.

If it’s better that changing a parameter such as max number requires multiple edits when it’s possible to require only one edit, perhaps you can share the rationale behind that.

For this project, if it’s done the way I did it then it’s very unlikely that the program will display the wrong numbers because the program has no other option. Doing it the other way has a possibility of error thus creating more work and more wasted time while you try and figure it out.

…for some though the wasted time etc will be a personal achievement as they worked through it and realise, like you have clearly, that there are other ways to do it, over coming the errors and being happy with the final result.

Well done all round :slight_smile:

If you are saying it’s lower than 500 then the intial winning condition which is = 500 is false.
so you try for lower and if your program goes to 499 it means it’s working as intended.

on point to make the max appear as 1000 is to add the +1 to the max at the end of the start function not at the start so it will view the correct number instead of guess between (1 and 1001) cause then 1001 will never be fullfilled.

Hey all,

New to the course and coding in general. Just a little bit confused about how this exactly works.

I understand the math that our code is doing, but what’s throwing me off is is how it’s defining our new standard for “guess” each time. At the top we’ve set “guess = 500” , but what in the code is allowing guess to change without conflicting with the original value of 500?

Another instance that is confusing me is, if we’ve established that “min = 1” and “guess = 500”, how does “min = guess” work? Isn’t that also saying 500 = 1?

Here’s how i’ve tried to work it out in my head:

On our first “if” statement for out UpArrow input where we have :

min = guess;
guess = (max + min) / 2;

I understand that is saying that if they press UP ARROW, our maximum remains 1000. And the next guess will be 875, and so on.

However, where I get lost is when we go from up to down.

max = guess
guess = (max + min) / 2;

The math here is that our new max is 750 and our minimum is now 500, giving it the next guess of 625. My question is, where in the code is it retaining the value of 500 for our minimum in this instance? How does the program know that our minimum is going to be 500? From the last input?

Just confused as to how the program is defining what it’s next minimum/maximum is going to be.

Privacy & Terms