Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 8th, 2009, 12:30 PM   #1
Grahamtheinternetbean
Newbie
 
Join Date: Sep 2009
Posts: 10
Rep Power: 0 Grahamtheinternetbean is on a distinguished road
Lightbulb 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 1-100
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?'
Grahamtheinternetbean is offline   Reply With Quote
Old Oct 8th, 2009, 2:06 PM   #2
Adak
Hobby Coder
 
Join Date: May 2006
Posts: 1,181
Rep Power: 10 Adak will become famous soon enough
Re: Pascal Programming/Algorithm.

Quote:
Originally Posted by Grahamtheinternetbean View Post
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 1-100
Ok so I created an algorithm (the following)

Let A = 1 and B = 100 //call it lo and hi, instead of A and B
N=A+B DIV 2 //call it mid instead of N
User then inputs 'too high', 'too low' or 'spot on'

If too low then let a=n (and loop back to N=N+B) //lo = mid + 1
If too high then let b=n (and loop back to N=A+N) //hi = mid - 1

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?'
Didn't you ever play the "guess the number" game when you were a kid? Same thing (if you were clever).

//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.
Adak is offline   Reply With Quote
Old Oct 8th, 2009, 2:15 PM   #3
Grahamtheinternetbean
Newbie
 
Join Date: Sep 2009
Posts: 10
Rep Power: 0 Grahamtheinternetbean is on a distinguished road
Re: Pascal Programming/Algorithm.

Thankyou for the Reply it was very helpful. But was the
Quote:
Same thing (if you were clever).
really necessary? I'm only learning...
Grahamtheinternetbean is offline   Reply With Quote
Old Oct 18th, 2009, 1:07 PM   #4
Grahamtheinternetbean
Newbie
 
Join Date: Sep 2009
Posts: 10
Rep Power: 0 Grahamtheinternetbean is on a distinguished road
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 1-100
  startnum:=1;
  endnum:=100;
  //End declaring my range from 1-100
  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:=(endnum-startnum) 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:=guessnum-1;
                  interval:=(endnum-startnum) DIV 2;
                    if (interval<1)
                    then
                      begin
                        interval:=1;
                      end;
                  guessnum:=guessnum-interval;
                  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.
Grahamtheinternetbean is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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 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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:52 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC