By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Simple assignment

P: 5
In my c++ class so far were up to chapter 8 which is do while loops for loops if/else if and nested ifs character arrays and everything basic before that.
Now my program in the loop section of the book is asking the user to guess a number between 1 and 100. My first guess must be 50 so I have written a case statement asking him if the number 50 or if its too high or too low . then my peers in class say that put If statements in their case statement to keep narrowing it down how would you go about doing this. I don't know how to go around coding this can someone please lead me in the right direction . Someone else a few minutes ago told me to then narrow it by the midpoint of 25 if its lower or 75 if higher and diving by two but this makes no sence to me. All I have written so far is the case statement and for
Case 1:
number; I have no idea how to put an if statement in a case and what I should have in the if statement. Oh keep in mind if anyone knows any shorter ways to do this it would be nice since I know I'll need to increment eventually somewhere. Can I please have the basics of the code for this program shown to me i'm totally lost.
Nov 1 '06 #1
Share this Question
Share on Google+
10 Replies


P: 63
iLL
You’re trying to implement a “binary search algorithm.” A binary search is a very well known and efficient algorithm.

I had to do that exact assignment last year. I remember it was harder then it sounded.

It’s a very well known algorithm, I’m sure if google something like “binary search algorithm,” you’ll be right on your way!

Oh! And I don’t think a case statement is what you need. Your going to need while/do loops with if statements.
Nov 1 '06 #2

P: 5
while( theirInput != "Yes" )
{

if ( "Yes" )
{
cout << "Great!";
break;
}
if( "Higher" )
{
lowerRange = guess;
guess = ( upperRange - lowerRange ) / 2;
guess += lowerRange;
}
else if( "Lower" )
{
upperRange = guess;
guess = ( upperRange - lowerRange ) / 2;
guess += lowerRange;
}
return 0;
}

Am I on the right track. What more am i missing besides the initilizing and the first cout statements?
Nov 1 '06 #3

P: 63
iLL
I feel a little bad for doing your assignment but... I want you to look at two lines; I labeled them “Line 1” and “Line2”

You need to figure out why I did it that way. I could just tell you, but I’m a student too and I need to do my own damn homework LOL


Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2.  
  3. void guessNum();
  4.  
  5. int main()
  6. {
  7.     std::cout<<"Guess a number from 1 to 100 and keep that number to yourself"<<std::endl;
  8.     guessNum();
  9.     std::cout<<"Ha ha, I guessed it"<<std::endl;
  10. }
  11. void guessNum()
  12. {
  13.     bool foundNum = false;
  14.     int lower = 0; // Line 1
  15.     int upper = 100;
  16.  
  17.     while(!foundNum)
  18.     {
  19.         int input = 0;
  20.         int guess = lower + ((upper-lower)/2); //Line 2
  21.  
  22.         std::cout<<"Is your number "<<guess<<"? (1=yes 2=less then that 3=greater then that) ";
  23.         std::cin>>input;
  24.  
  25.         switch (input)
  26.         {
  27.         case 1:
  28.             foundNum = true;
  29.             break;
  30.         case 2:
  31.             upper = guess;
  32.             break;
  33.         case 3:
  34.             lower = guess;
  35.             break;
  36.         }
  37.  
  38.     }
  39. }
Nov 1 '06 #4

P: 63
iLL
Oh.... LOL


I know I told you that you didn’t need a switch statement.

I guess I was wrong
Nov 1 '06 #5

P: 5
I feel a little bad for doing your assignment but... I want you to look at two lines; I labeled them “Line 1” and “Line2”

You need to figure out why I did it that way. I could just tell you, but I’m a student too and I need to do my own damn homework LOL


Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2.  
  3. void guessNum();
  4.  
  5. int main()
  6. {
  7.     std::cout<<"Guess a number from 1 to 100 and keep that number to yourself"<<std::endl;
  8.     guessNum();
  9.     std::cout<<"Ha ha, I guessed it"<<std::endl;
  10. }
  11. void guessNum()
  12. {
  13.     bool foundNum = false;
  14.     int lower = 0; // Line 1
  15.     int upper = 100;
  16.  
  17.     while(!foundNum)
  18.     {
  19.         int input = 0;
  20.         int guess = lower + ((upper-lower)/2); //Line 2
  21.  
  22.         std::cout<<"Is your number "<<guess<<"? (1=yes 2=less then that 3=greater then that) ";
  23.         std::cin>>input;
  24.  
  25.         switch (input)
  26.         {
  27.         case 1:
  28.             foundNum = true;
  29.             break;
  30.         case 2:
  31.             upper = guess;
  32.             break;
  33.         case 3:
  34.             lower = guess;
  35.             break;
  36.         }
  37.  
  38.     }
  39. }
Can you write that without using void and guessNum() and bool we havn't learnd those yet this is very basic c++. Oh and the program above should work also. But i'd like to try it your way . I still dont have a compiler what do you use? Dev c++ doenst like me and microsoft express VS doesnt save files to the right place so I can't run squat. Please help me get a simple c++ compiler.
Nov 1 '06 #6

P: 63
iLL
I use Microsoft Visual Studio 2005 because it is free to use through my school. The express edition should work the same way as the full version. It saves all of the source in a subdirectory with the same name as the project. Example, if your projects name is “Prog,” your source would be in Prog/Prog.

If you are having trouble finding your project folder, the default is in My Documents\Visual Studio\Projects

Any compiler should work though. Just find a free C++ IDE that comes with a complier. Just google “free C++ IDE”


Now about the code I wrote for you -

The code I gave you is more of a guide line. Just figure out what is going on and you will easily be able to recreate it in any way you want.

If you like, you can just delete the line guessNum(); from main(), then copy and past all of the code from that function in the place of where guessNum(); was

Such as
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2.  
  3. int main()
  4. {
  5.     std::cout<<"Guess a number from 1 to 100 and keep that number to yourself"<<std::endl;
  6.  
  7.     //start of guessNum()
  8.     bool foundNum = false;
  9.     int lower = 0; // Line 1
  10.     int upper = 100;
  11.  
  12.     while(!foundNum)
  13.     {
  14.         int input = 0;
  15.         int guess = lower + ((upper-lower)/2); //Line 2
  16.  
  17.         std::cout<<"Is your number "<<guess<<"? (1=yes 2=less then that 3=greater then that) ";
  18.         std::cin>>input;
  19.  
  20.         switch (input)
  21.         {
  22.         case 1:
  23.             foundNum = true;
  24.             break;
  25.         case 2:
  26.             upper = guess;
  27.             break;
  28.         case 3:
  29.             lower = guess;
  30.             break;
  31.         }
  32.  
  33.     }
  34.     //end of guessNum()
  35.  
  36.     std::cout<<"Ha ha, I guessed it"<<std::endl;
  37. }
  38.  
I don’t see why you can’t use a bool variable. It’s just a data type, like int, double, or char. In fact, I believe that a bool is an int that holds a 0 for false or a 1 for true.
Nov 2 '06 #7

P: 5
This why seems much easier http://rafb.net/paste/results/xPLpzt16.html . But since I can't compile it can you do it for me and tell me if it works correctly. I did it in my head and it makes sense to me do you think it would work and are there any adjustments you would make to it in your opinion?
Nov 2 '06 #8

P: 5
Um I have code blocks with the MIN compiler how do I get it to open source file i don't know what type of file to open EDIT ALL I can do is compile it but no build option.
Nov 2 '06 #9

P: 63
iLL
This why seems much easier http://rafb.net/paste/results/xPLpzt16.html . But since I can't compile it can you do it for me and tell me if it works correctly. I did it in my head and it makes sense to me do you think it would work and are there any adjustments you would make to it in your opinion?
No

Get a damn compiler
Nov 2 '06 #10

P: 63
iLL
Um I have code blocks with the MIN compiler how do I get it to open source file i don't know what type of file to open EDIT ALL I can do is compile it but no build option.
Sorry, never used code::blocks
Nov 2 '06 #11

Post your reply

Sign in to post your reply or Sign up for a free account.