445,750 Members | 1,187 Online
+ 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
10 Replies

 P: 63 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 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 #include   void guessNum();   int main() {     std::cout<<"Guess a number from 1 to 100 and keep that number to yourself"<>input;           switch (input)         {         case 1:             foundNum = true;             break;         case 2:             upper = guess;             break;         case 3:             lower = guess;             break;         }       } } Nov 1 '06 #4

 P: 63 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 #include   void guessNum();   int main() {     std::cout<<"Guess a number from 1 to 100 and keep that number to yourself"<>input;           switch (input)         {         case 1:             foundNum = true;             break;         case 2:             upper = guess;             break;         case 3:             lower = guess;             break;         }       } } 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 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 #include   int main() {     std::cout<<"Guess a number from 1 to 100 and keep that number to yourself"<>input;           switch (input)         {         case 1:             foundNum = true;             break;         case 2:             upper = guess;             break;         case 3:             lower = guess;             break;         }       }     //end of guessNum()       std::cout<<"Ha ha, I guessed it"<

 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 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 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