Not PLC Related, but Programming Related

Join Date
Apr 2002
Location
Burlington, Ontario
Posts
186
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
 
Two Points

Andrew, you how learn how to use the debugger to single step through the your program. Some PLCs allow single stepping too but be carefull. Single stepping is very useful when debugging algorithms. Single stepping is not as useful and may be dangerous when debugging on a real machine.

Terry. This is the kind of program that can be done using recursion.
That doesn't mean that recursion is the best way.

Code:
#include (iostream.h) 

int guess(lo,hi) 
{ 
  int mid = ( lo + hi ) >> 1; 
  char response;
 
  cout << "Is your number is" << mid << " ?\n"; 
  cin >> response; 
  switch(response) 
  { 
    case 'l' : guess(lo,mid);   // RECURSIVE CALL
    break; 
    case 'h' : guess(mid,hi);   // RECURSIVE CALL
    break; 
    case 'y' : return mid; 
  } 
  return(mid); 
}

int main()
{
  int answer;
  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"; 
  answer = guess(1,100);
  cout << " Your number is: : ",guess
}

This is for informative purposes only as an example of a recusive call. Andrew's solution is more efficient and there is a still small problem with my program. I will leave this as an execise.

Back on topic. I think SLCs single step. I will check Control Logix.
 

Similar Topics

I need to know how to take a schematic like this and solve it for let’s say a run time of 5 seconds By solve I mean determine which rungs are...
Replies
33
Views
8,977
What's your favourite source of PLC/SCADA-related news? Maybe some of you still read printed magazines?
Replies
4
Views
2,049
Good morning all, this is not PLC related so if not allowed just delete. But i have a problem with my RV that has got me stumped big time. I have...
Replies
12
Views
3,684
Hi to all..!!! I am planning to prepare disaster recovery/Business continuity process documents for PLC & Scada system. I need some sample...
Replies
2
Views
1,791
Hello All, It's past that time where my membership ran out, so I've come to do a Lifetime upgrade this time... but it won't let me. It keeps...
Replies
3
Views
2,411
Back
Top Bottom