One question, are the comments "Needs to go lower" both supposed to say the same thing? |
No, the second one is supposed to say Needs to go higher
Also, what's the deal with the "guess = origRnum" at the top? |
The purpose behind that is so that I could rearrange your cout statements and also allow you to remove the cout statements and hiLow checks before the while statement. If the computer guesses right on the first try, you're screwed because you have to respond if it was higher or lower, but it's the same.
This also allows you to simplify your code down to only what's needed, nothing more.
And after an "if' statement, you don't need to put '{' and '}' in them? |
No, if an if/else is missing it's brackets, it will read the next statement only. I'll teacher you something, observe:
1 2 3 4 5 6
|
if (something)
Do something;
else if (something else)
Do something else;
else
Do everything else;
|
Actually translates to this:
1 2 3 4 5 6 7
|
if (something)
Do something;
else
if (something else)
Do something else;
else
Do everything else;
|
Now with brackets:
1 2 3 4 5 6 7 8 9
|
if (something) {
Do something;
}
else if (something else) {
Do something else;
}
else {
Do everything else;
}
|
Same thing as:
1 2 3 4 5 6 7 8 9 10 11
|
if (something) {
Do something;
}
else {
if (something else) {
Do something else;
}
else {
Do everything else;
}
}
|
Something to take notice to in both of the "same thing as" is the fact that there is no else if's. This is because the term else if doesn't exist in C++. It's really a twist on the unlimited zero space allowed by the language, and the use of the bracketless if/else's. To put it simply, C++ requires all else's to have exactly one if, and every if can't have more than one else. else if's don't exist. This is proof that you have been using bracketless if/else statements without even knowing it ;)
So the variable 'guess' seeds the 'hival' and 'lowval' variables the numbers to be inputted after the 'if' statement correct? |
Yes and no. The purpose of hival and lowval is to limit the options the computer can guess. Do yourself a favor and grab a pen and a piece of paper and follow your code as it does it. You know what guess is equal to because you display it each time. You also can tell what hi/lowval is going to be because of the if statements, and you know what your number is since you choose it. See if it makes sense during a run of your code.
Sometimes if you're unsure how exactly things work, paper and pencil/pen work wonders. I still use that technique today to debug my code when I'm confused on how it should behave. Take this code I just finished:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
template<class T>
void vp::list<T>::sort() {
if (size() <= 1)
return;
for (int merges = 0, k = 1; merges != 1; k *= 2) {
node *p = firstNode, *q = p;
for (int qsize = merges = 0; p; merges ++, p = q) {
for (qsize = 0; qsize < k && q; qsize ++, q = q->next);
while (qsize && q) {
if (q->value <= p->value) {
if (q->next) {
q = q->next;
q->prev->insert_before(p);
}
else {
q->insert_before(p);
q = NULL;
lastNode = lastNode->prev;
}
if (p == firstNode)
firstNode = firstNode->prev;
qsize --;
}
else
p = p->next;
}
}
}
}
|