okay, to start off I am a complete beginner in a compSci class. I could ask my teacher, but I don't have time to do so. So, expect some really dumb errors that I can't see and FYI, I am using eclipse.
here's my code:
#include <iostream>
using namespace std;
int something()
{
int big=1000;//largest number is 1000
int small=1;//smallest number is 1
//so, best guess is to go in the middle
int c;//my guesses
int inequality;//used to write if statements
for (int a=0; a <= 10;a++)
{
cout << "think about a number between 1-1000" << endl;//what consul tells you
c=(big - small)/2;//my guess will be the midpoint of the two numbers
while (big > small)//while the highest number is ALWAYS greater than the lowest number...
{
cout << "is your number less than, equal to, or greater than my guess? 1-less,2-equal,3-greater" << c << endl;
cin >> inequality;//you tell me whether my guess is too high, low, or equal
if (inequality == 1)//if you say it is too low...
{
small = c;//the smallest number is now my last guess
c = c - (big - small)/2;//so, ill take the midpoint of the CURRENT biggest and smallest number
}
else if (inequality == 2)//if you say it is equal...
{
cout << "yay i guessed your number." << endl;//cool.
}
else if (inequality == 3)//if you say it is too high...
{
big = c;//biggest number is now my guess
c=c+(big-small)/2;//so, ill take the midpoint of the CURRENT biggest and smallest number
}
}
}
system("pause");
return 0;//returns something in int main function
}
int main()
{
something();//so i can actually do code.
}
So my problem: if I enter 1 after the consul enters the first guess, I get 499, which is fine. After the second guess (where I enter 1), I get 249, which is also fine. However, the third guess after I enter 1 gets a random 681, so could someone help me?
btw, it would be most appreciated if you did not rewrite the entire code for me, otherwise that is really suspicious when I turn it in. I am struggling because I do not have very good compSci background, so to improve, I need ideas mostly. Lastly, any way to make my code look a LITTLE more professional would be appreciated :)
also, my for loop may be a bit off, I'm not sure.
Aucun commentaire:
Enregistrer un commentaire