473,322 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Simple assignment

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

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

Similar topics

6
by: Arthur J. O'Dwyer | last post by:
I was paging through Coplien's book "Advanced C++ Programming Styles and Idioms" this afternoon and found some code that looked something like void sort(vector<foo> a) { int flip; do { for...
11
by: Frag | last post by:
Hi guys, I searched around without any clear answer. Tons of stuff, but nothing concrete. I am trying to call this Javascript function: function ButtonClicked() { alert("The button has...
17
by: savesdeday | last post by:
In my beginnning computer science class we were asked to translate a simple interest problem. We are expected to write an algorithm that gets values for the starting account balance B, annual...
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
7
by: bfowlkes | last post by:
Hi, I am new to the C programming language and programming in general. I am writing a simple roulette program. I have everything working so far but it seems a little plain. What I would like to...
7
by: Dom | last post by:
<%# Session("oID") = Container.DataItem("VendorID")%> The above statment, writes the value of the 'VendorID' instead of assigning it to the session variable. What's wrong? I'm a classic ASP...
13
by: Mike S | last post by:
I came across the following paragraph in the "Semantics" section for simple assignment in N1124 (C99 draft) and I'm wondering if I'm interpreting it right: 6.5.16.1p3: If the value being...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
7
by: isinc | last post by:
I have an assignment that I'm working on and am having trouble. Not too familiar with graphics. Any help/guidance would be much appreciated to see if what I have so far is okay and what I should do...
13
by: John Dann | last post by:
A Python newbie, but some basic understanding of how classes, objects etc work in eg VB.Net. However, I'm struggling a little to translate this knowledge into the Python context. I'm trying to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.