Andrew Evenson
Member
Hi all,
I know this isnt a PLC related question, but I hope I can get some feedback. I'm taking a C++ programming course at night school(OK, so it can be applied to PLC ), and have an assignment, in which I cannot get it to work. I understand how the algorithm work, but can't seem to do what I want in the C++ environment. I'm trying to create a "previous" value, as you would in the PLC, ie...as in calculation Speed, in where you would look at the delta change in counts off an encoder card.
My problem is to write a program to guess a number the user picks between 1 - 100. The inital guess is 50. From there, the user indicates if their number higher or lower. OK, no problem.
The algorithms I have come up with are as follows:
Each time the program divides the working range of number is half, to zero in on the guessed number.
If the number is higher, GUESS = GUESS + (PREVIOUS_GUESS - GUESS)/2
If the number is lower, GUESS = GUESS - (GUESS - PREVIOUS_GUESS)/2
EX.
Initially: previous_guess = 0.
guess is 50.
Is 50 your number?? - too high.
so guess = 50 - (100 - 50)/2 = 25
guessA = previous _guess.
Is 25 your number? - too low.
guess = 25 + (50 -25)/2 = 37.5..
Do you see what Im trying to accomplish?? How do I create that previous_guess?
Here is the code I have come up with so far..
#include (iostream.h)
int main()
{
int guess = 50;
int intern_guess = 50;
int prev_guess = 0;
char response;
do
{
cout << "This program will guess a number you \n";
cout << "pick between 1 and 100 \n";
cout << "If the guess is too high, press [l] \n";
cout << "If the guess is too low, press [h] \n";
cout << "If the guess is correct, press [y] \n";
cout << "\n";
guess = intern_guess;
cout << "Is your number " << guess << " ?\n";
cin >> response;
switch(response)
{
case 'l' : intern_guess = guess - (prev_guess - guess)/2;
break;
case 'h' : intern_guess = guess + (guess - prev_guess)/2;
break;
case 'y' : cout << "Your number was " << guess << " ?";
}
}while (response != 'y');
return(0);
}
I know the brackets around the #include statement shoulf be "less than and greater than" symbols, but system wouldnt let me type them in that way..
So, am I out to lunch??? If anyone could be of some assistance I would really appreciate it.
I hope no one is ticked off because this is a none Ladder logic question.
Andrew Evenson
I know this isnt a PLC related question, but I hope I can get some feedback. I'm taking a C++ programming course at night school(OK, so it can be applied to PLC ), and have an assignment, in which I cannot get it to work. I understand how the algorithm work, but can't seem to do what I want in the C++ environment. I'm trying to create a "previous" value, as you would in the PLC, ie...as in calculation Speed, in where you would look at the delta change in counts off an encoder card.
My problem is to write a program to guess a number the user picks between 1 - 100. The inital guess is 50. From there, the user indicates if their number higher or lower. OK, no problem.
The algorithms I have come up with are as follows:
Each time the program divides the working range of number is half, to zero in on the guessed number.
If the number is higher, GUESS = GUESS + (PREVIOUS_GUESS - GUESS)/2
If the number is lower, GUESS = GUESS - (GUESS - PREVIOUS_GUESS)/2
EX.
Initially: previous_guess = 0.
guess is 50.
Is 50 your number?? - too high.
so guess = 50 - (100 - 50)/2 = 25
guessA = previous _guess.
Is 25 your number? - too low.
guess = 25 + (50 -25)/2 = 37.5..
Do you see what Im trying to accomplish?? How do I create that previous_guess?
Here is the code I have come up with so far..
#include (iostream.h)
int main()
{
int guess = 50;
int intern_guess = 50;
int prev_guess = 0;
char response;
do
{
cout << "This program will guess a number you \n";
cout << "pick between 1 and 100 \n";
cout << "If the guess is too high, press [l] \n";
cout << "If the guess is too low, press [h] \n";
cout << "If the guess is correct, press [y] \n";
cout << "\n";
guess = intern_guess;
cout << "Is your number " << guess << " ?\n";
cin >> response;
switch(response)
{
case 'l' : intern_guess = guess - (prev_guess - guess)/2;
break;
case 'h' : intern_guess = guess + (guess - prev_guess)/2;
break;
case 'y' : cout << "Your number was " << guess << " ?";
}
}while (response != 'y');
return(0);
}
I know the brackets around the #include statement shoulf be "less than and greater than" symbols, but system wouldnt let me type them in that way..
So, am I out to lunch??? If anyone could be of some assistance I would really appreciate it.
I hope no one is ticked off because this is a none Ladder logic question.
Andrew Evenson