


Thread Tools  Display Modes 
Oct 8th, 2009, 12:30 PM  #1 
Newbie
Join Date: Sep 2009
Posts: 10
Rep Power: 0

Pascal Programming/Algorithm.
Ok so basically I have the task:
Write a program to let the computer guess what number YOU have thought of within a specified range. I am choosing that range to be 1100 Ok so I created an algorithm (the following) Let A = 1 and B = 100 N=A+B DIV 2 User then inputs 'too high', 'too low' or 'spot on' If too low then let a=n (and loop back to N=N+B) If too high then let b=n (and loop back to N=A+N) Etc... I can see the problems that would cause and I know its not right. But I just can't get my head around the problem. If people could submit algorithms I would be grateful. I don't want the program written I just can't get my head round it. I got this advice from someone... ' algorithm is basically flawed. The challenge is to reset the range of random numbers generated to exclude the number guessed and either the ones above it or below it depending on whether the guess is too high or too low. Look at this example with the number to be guessed set at 43: If the computer guesses 72 then the guess is too high. This means that the next random number generated (ie the next guess) has to exclude 72 and all the numbers above it. Basically the random number has to be in the range 1 to 71 (you should be able to work this out with the data that you already have stored) If however, the computer guesses 34 then the guess is too low. This gives a more difficult problem as the new random number generated has to exclude 43 and all the numbers below it, ie a random number in the range 35 to 100. This is a more difficult problem as random numbers are generated from ONE. Can you think how you might do this?' 
Oct 8th, 2009, 2:06 PM  #2  
Hobby Coder
Join Date: May 2006
Posts: 1,181
Rep Power: 13

Re: Pascal Programming/Algorithm.
Quote:
//sorry, I don't know pascal, so this is pseudo C int mid; //lo and hi are parameters to the function, generally. lo = lowest number in range hi = highest number in range while(lo <= hi) { mid = (lo + hi) / 2 if(guess > goal) //as above, adjusting the lo or hi else if(guess < goal) //as above, adjusting the lo or hi else return 1; //goal was found } return 0; //goal was not in the array This is the famous binary search. It's helpful if you use the common variable names. 

Oct 8th, 2009, 2:15 PM  #3  
Newbie
Join Date: Sep 2009
Posts: 10
Rep Power: 0

Re: Pascal Programming/Algorithm.
Thankyou for the Reply it was very helpful. But was the
Quote:


Oct 18th, 2009, 1:07 PM  #4 
Newbie
Join Date: Sep 2009
Posts: 10
Rep Power: 0

Re: Pascal Programming/Algorithm.
My end Solution (Not fully finished but I probably wont add anymore)
I know it would have been quicker, more efficient and much better programming to use while loops. I also know that the method I used to calculate the margins also means that if the number is like 100. and i keep going higher higher higher. eventually my program starts going 96,97,98,99,100 until it gets to 100. But yeah. Here we go. Thought I would just post it for future people on the forums. program computationalguess; {$APPTYPE CONSOLE} Var realnum:integer; Var guessnum:integer; Var startnum:integer; Var endnum:integer; Var interval:integer; //We need interval to calculate the margins e.g. 100 and 1. Var usinput:char; Var gotit:integer; Begin //Start declaring my range from 1100 startnum:=1; endnum:=100; //End declaring my range from 1100 Writeln('Please declare your number now. '); Write('Input here: '); readln(realnum); Writeln; Writeln('Starting Guessing program now...'); Writeln('Please use "l"  Lower, "h"  Higher and "y" for correct'); guessnum:=endnum div 2; Writeln; write('Is your number? : ',guessnum,' : '); Readln(usinput); Repeat If (guessnum<>realnum) Then Begin Begin If (usinput='h') //Higher then begin if (usinput='h') and (guessnum<>realnum) Then Begin startnum:=guessnum+1; interval:=(endnumstartnum) DIV 2; if (interval<1) then begin interval:=1 end; guessnum:=guessnum+interval; Write('Is your number? ',guessnum,': '); Readln(usinput); End; if (usinput='h') and (guessnum=realnum) Then Begin Writeln; Writeln('Why are you cheating and trying to ruin the fun'); Writeln; gotit:=1; End Else if (usinput='h') and (guessnum>realnum) Then Begin Writeln; Writeln('Why are you cheating and trying to ruin the fun'); Writeln; gotit:=1; End end; If (usinput='l') //Lower then begin if (usinput='l') and (guessnum<>realnum) then Begin endnum:=guessnum1; interval:=(endnumstartnum) DIV 2; if (interval<1) then begin interval:=1; end; guessnum:=guessnuminterval; Write('Is your number? ',guessnum,': '); Readln(usinput); end; if (usinput='l') and (guessnum=realnum) Then Begin Writeln; Writeln('Why are you cheating and trying to ruin the fun'); Writeln; gotit:=1; End Else if (usinput='l') and (guessnum<realnum) Then Begin Writeln; Writeln('Why are you cheating and trying to ruin the fun'); Writeln; gotit:=1; End; end; If (usinput='y') //Correct Then Begin If (usinput='y') and (realnum=guessnum) Then Begin Writeln; Writeln('I told you I would get it! Im a G'); Writeln; end Else if (usinput='y') and (realnum<>guessnum) Then Begin Writeln; Writeln('Why are you cheating and trying to ruin the fun'); Writeln; Gotit:=1 end End End End; Until (usinput='y') or (gotit=1); Writeln; Writeln(''); Writeln('Thankyou for using this program!'); Writeln(''); Writeln; Readln; //Obviously I know there are some bugs in it and I can't fix them at the moment //But on the whole it works as a solution to the problem and stops some cheating. End. 
Bookmarks 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)  
Thread Tools  
Display Modes  


Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Pascal?  Indigno  Other Programming Languages  14  Sep 24th, 2006 2:38 AM 
Pascal or C OS  bigguy  Coder's Corner Lounge  15  Feb 27th, 2006 5:20 PM 
Images in TURBO PASCAL  S_A_L_M_A_N  Other Programming Languages  4  Dec 20th, 2005 9:38 AM 
Need help badly: Pascal conversion to C  joan400  C  0  Apr 12th, 2005 9:48 PM 
Delphi as opposed to Pascal?  peace_of_mind  Delphi  8  Apr 2nd, 2005 8:23 AM 