You are not registered yet. Please click here to register!


 
 
plc storereviewsdownloads
This board is for PLC Related Q&A ONLY. Please DON'T use it for advertising, etc.
 
Try our online PLC Simulator- FREE.  Click here now to try it.

---------->>>>>Get FREE PLC Programming Tips

New Here? Please read this important info!!!


Go Back   PLCS.net - Interactive Q & A > PLCS.net - Interactive Q & A > LIVE PLC Questions And Answers

PLC training tools sale

Reply
 
Thread Tools Display Modes
Old June 25th, 2002, 07:41 AM   #1
Andrew Evenson
Member
Canada

Andrew Evenson is offline
 
Andrew Evenson's Avatar
 
Join Date: Apr 2002
Location: Burlington, Ontario
Posts: 184
Not PLC Related, but Programming Related

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
  Reply With Quote
Old June 25th, 2002, 08:34 AM   #2
bernie_carlton
Lifetime Supporting Member + Moderator
United States

bernie_carlton is offline
 
bernie_carlton's Avatar
 
Join Date: Apr 2002
Location: Yakima, Washington
Posts: 6,335
place

prev_guess = guess

after the close curly_brackets of the 'switch' statement
  Reply With Quote
Old June 25th, 2002, 09:13 AM   #3
Peter Nachtwey
Member
United States

Peter Nachtwey is offline
 
Peter Nachtwey's Avatar
 
Join Date: Apr 2002
Location: Vancouver, WA, US
Posts: 7,049
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.
  Reply With Quote
Reply
Jump to Live PLC Question and Answer Forum

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Topics
Thread Thread Starter Forum Replies Last Post
Non PLC related, but looking for your input SLaubach LIVE PLC Questions And Answers 17 January 17th, 2011 11:26 AM
Question in programming Siemens PLC.. stevenPLC LIVE PLC Questions And Answers 9 September 22nd, 2004 11:21 AM
Help with PLC Programming ecope LIVE PLC Questions And Answers 16 April 16th, 2004 07:09 AM
PLC programming course pxs LIVE PLC Questions And Answers 9 March 23rd, 2004 01:50 AM
DL305 (TI305) PLC programming problems pambridge LIVE PLC Questions And Answers 5 March 11th, 2004 10:12 AM


All times are GMT -5. The time now is 08:09 AM.


.