Board index » delphi » Need help to improve a program in TP

Need help to improve a program in TP

                            Hello everybody!

    I  need your help to improve the calcul method of a program.
This program is called "le compte est bon" and consist in making the
computer
calculate  a  given random number with six other numbers ("plaquettes")
and
the operations {*, /,+, -}. It uses each of the six numbers only once
and if it do
not find, gives the best approximation.
I've already done something, but the program is not "intelligent"
enough.
    So, if you have ideas, put your modifications in an other color in
the source
and send it back, thank you!

Here's the source code:

PROGRAM Le_compte_est_bon;

uses crt;

type table=array [100..999] of integer;

type sac=array [1..69] of integer;

type position=array[1..6] of integer;
var p:position;

type fableau=array[1..15] of integer;
var f:fableau;

type gableau=array[1..4] of integer;
var g:gableau;

type hableau=array[1..18] of integer;
var h:hableau;

type qableau=array[1..4] of integer;
var q:qableau;

var nb,min,minus,micro,u,v:integer;

Procedure remplit_sac (var s:sac);
var k:integer;
begin
     for k:=1 to 10 do s[k]:=1;
     for k:=11 to 20 do s[k]:=2;
     for k:=21 to 25 do s[k]:=3;
     for k:=26 to 30 do s[k]:=4;
     for k:=31 to 35 do s[k]:=5;
     for k:=36 to 40 do s[k]:=6;
     for k:=41 to 45 do s[k]:=7;
     for k:=46 to 50 do s[k]:=8;
     for k:=51 to 55 do s[k]:=9;
     for k:=56 to 59 do s[k]:=10;
     for k:=60 to 62 do s[k]:=25;
     for k:=63 to 65 do s[k]:=50;
     for k:=66 to 67 do s[k]:=75;
     for k:=68 to 69 do s[k]:=100;
end;

Procedure Nombre_a_trouver;
begin
     randomize;
     nb:=random (900)+100;
     writeln('Le nombre trouver est  ',nb,
             ' ,appuyez sur <ENTREE> pour avoir la solution');
     readln;
end;

PROCEDURE tirage_des_six_plaquettes(s:sac);
var i,j:integer;
    alea:integer;
    appartient:boolean;

begin
randomize;
for j:=1 to 6 do p[j] := 0;
j:=1;

repeat
      alea:=random (69)+1;
      appartient:=false;
      i:=1;
      while (not(appartient)) and (i<>j) do
            begin
                 if (alea=p[i]) then appartient:=true
                 else i:=i+1;
            end;

      if (not(appartient)) then
         begin
              p[j] := alea;
              j := j+1;
         end;
until (j=6);

write('Nombres tir?s : ');
for j:=1 to 6 do write(' | ',s[p[j]],' | ');
writeln;
end;

PROCEDURE calcul;
var i:integer;

begin
for i:=1 to 5 do
    f[i]:=abs(nb-p[i]*p[i+1]);
for i:=1 to 4 do
    f[i+5]:=abs(nb-p[i]*p[i+2]);
for i:=1 to 3 do
    f[i+9]:=abs(nb-p[i]*p[i+3]);
for i:=1 to 2 do
    f[i+12]:=abs(nb-p[i]*p[i+4]);
f[15]:=abs(nb-p[1]*p[6]);
if f[1]<f[2] then
   min:=f[1] else min:=f[2];
for i:=3 to 15 do
    begin
         if f[i]<min then min:=f[i] else min:=min;
    end;
end;

PROCEDURE minimum;
begin
     if min=0 then writeln ('YAHOO ! Le compte est bon !');
end;

PROCEDURE elimination ;
var i:integer;

begin
if min=f[1] then for i:=1 to 4 do g[i]:=p[i+2];
if min=f[2] then
   begin
        g[1]:=p[1];
        for i:=2 to 4 do g[i]:=p[i+2];
   end;
if min=f[3] then
   begin
        for i:=1 to 2 do g[i]:=p[i];
        for i:=3 to 4 do g[i]:=p[i+2];
   end;
if min=p[4] then
   begin
        for i:=1 to 3 do g[i]:=p[i];
        g[4]:=p[6];
   end;
if min=f[5] then
   for i:=1 to 4 do g[i]:=p[i];
if min=f[6] then
   begin
        g[1]:=p[2];
        for i:=2 to 4 do g[i]:=p[i+2];
   end;
if min=f[7] then
   begin
        g[1]:=p[1];
        g[2]:=p[2];
        for i:=3 to 4 do g[i]:=p[i+2];
   end;
if min=f[8] then
   begin
        for i:=1 to 2 do g[i]:=p[i];
        g[3]:=p[4];
        g[4]:=p[6];
   end;
if min=f[9] then
   begin
        for i:=1 to 3 do g[i]:=p[i];
        g[4]:=p[5];
   end;
if min=f[10] then
   begin
        for i:=1 to 2 do g[i]:=p[i+1];
        for i:=3to 4 do g[i]:=p[i+2];
   end;
if min=f[11] then
   begin
        g[1]:=p[1];
        for i:= 2 to 3 do g[i]:=p[i+1];
        g[4]:=p[6];
   end;
if min=f[12] then
   begin
        for i:=1to 2 do g[i]:=p[i];
        for i:= 3 to 4 do g[i]:=p[i+1];
   end;
if min=f[13] then
   begin
        for i:=1to 3 do g[i]:=p[i+1];
        g[4]:=p[6];
   end;
if min=f[14] then
   begin
        g[1]:=p[1];
        for i:=2 to 4 do g[i]:=p[i+1];
   end;
if min=f[15] then
   for i:=1 to 4 do g[i]:=p[i+1];

end;

PROCEDURE calcul2;
var i:integer;

begin
if min>0 then
   begin
        for i:=1 to 3 do
            h[i]:=abs(min-(g[i]+g[i+1]));
        for i:=1 to 2 do
            h[i+3]:=abs(min-(g[i]+g[i+2]));
        h[6]:=abs(min-(g[1]+g[4]));
        for i:=1 to 3 do
            h[i+6]:=abs(min-(g[i]*g[i+1]));
        for i:=1 to 2 do
            h[i+9]:=abs(min-(g[i]*g[i+2]));
        h[12]:=abs(min-(g[1]*g[4]));
        for i:=1to 3 do
            h[i+12]:=abs(min-(g[i]-g[i+1]));
        for i:=1 to 2 do
            h[i+15]:=abs(min-(g[i]-g[i+2]));
        h[18]:=abs(min-(g[1]-g[4]));
        if h[1]<h[2] then
           minus:=h[1] else minus:=h[2];
        for i:=3 to 15 do
            begin
                 if h[i]<minus then
                    minus:=h[i] else
                    minus:=minus;
            end;
   end;
end;

PROCEDURE minimal;
begin

if min>0 then
   begin
        if minus=0 then writeln ('YAHOO ! Le compte est bon !');
   end;
end;

PROCEDURE elimination2;
var i:integer;

begin
for i:=0 to 3 do
    begin if minus=h[1+6*i] then
          begin
               u:=g[3];
               v:=g[4];
          end;
    end;
for i:=0 to 3 do
    begin
         if minus=h[2+6*i] then
            begin
                 u:=g[1];
                 v:=g[4];
            end;
    end;
for i:=0 to 3 do
    begin
         if minus=h[3+6*i] then
            begin
                 u:=g[1];
                 v:=g[2];
            end;
    end;
for i:=0 to 3 do
begin
     if minus=h[4+6*i] then
        begin
             u:=g[2];
             v:=g[4];
        end;
end;
for i:=0 to 3 do
    begin
         if minus=h[5+6*i] then
            begin
                 u:=g[1];
                 v:=g[3];
            end;
    end;
for i:=0 to 3 do
    begin
         if minus=h[6+6*i] then
            begin
                 u:=g[2];
                 v:=g[3];
            end;
    end;
end;

PROCEDURE calcul3;
var i:integer;

begin
if min>0 then
   begin
        if min>0 then
           begin
                q[1]:=abs(minus-u*v);
                q[2]:=abs(minus-(u+v));
                q[3]:=abs(minus-(u-v));
                q[4]:=abs(minus-(v-u));
                if g[1]<g[2] then
                   micro:=g[1] else
                   micro:=g[2];
                if g[3]<micro then
                   micro:=g[3] else
                   micro:=micro;
           end;
   end;
end;

PROCEDURE finale;
begin
     if min>0 then
        begin
             if min>0 then
                begin
                     while micro<0 do calcul3;
                     if micro=0 then
                        writeln ('YAHOO ! Le compte est bon !');
                     if micro>0 then
                        writeln ('Le nombre le plus proche est :
',nb-micro);
                end;
        end;
end;

var s1:sac;
var o:string[1];

begin
     clrscr;
     remplit_sac (s1);
     tirage_des_six_plaquettes (s1);
     nombre_a_trouver;
     calcul;
     minimum;
     elimination;
     calcul2;
     minimal;
     elimination2;
     calcul3;
     finale;
     Writeln;
     Writeln('Voulez-vous faire un autre compte ? (o/n)');
     readln(o);
     while o<>'n' do
           begin
                clrscr;
                remplit_sac (s1);
                tirage_des_six_plaquettes (s1);
                nombre_a_trouver;
                calcul;
                minimum;
                elimination;
                calcul2;
                minimal;
                elimination2;
                calcul3;
                finale;
                Writeln;
                Writeln('Voulez-vous faire un autre compte ? (o/n)');
                readln(o);
           end;
     if o='n' then writeln('Au revoir...');
     readln;

end.

 

Re:Need help to improve a program in TP


In article <36DEA975.A0297...@caramail.com>,
  Gab <Ga...@caramail.com> wrote:

Quote
>                             Hello everybody!

>     I  need your help to improve the calcul method of a program.
> This program is called "le compte est bon" and consist in making the
> computer
> calculate  a  given random number with six other numbers ("plaquettes")
> and
> the operations {*, /,+, -}. It uses each of the six numbers only once
> and if it do
> not find, gives the best approximation.
> I've already done something, but the program is not "intelligent"
> enough.
>     So, if you have ideas, put your modifications in an other color in
> the source
> and send it back, thank you!

This is not a pizza shop, if you post here, you must be prepared to pick up
the solution here!

Quote
> Here's the source code:

<snipped>

Aaaaagh!!!! Shudder! The same thing again in HTML.

<snipped>

I can't help you with your problem, but can you please stop sending the same
thing in HTML. I access the net at work, so it doesn't cost me anything but
time, others have to use the phone and postings like yours only help to push
up their phone bill!

AND DON'T EVER AGAIN POST THE SAME POSTING TO SEVEN GROUPS, CROSSPOST!!!!!

Robert
--
Robert AH Prins
prin...@williscorroon.com

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Re:Need help to improve a program in TP


Robert AH Prins a crit :

Quote

> This is not a pizza shop, if you post here, you must be prepared to pick up
> the solution here!

> > Here's the source code:
> <snipped>

> Aaaaagh!!!! Shudder! The same thing again in HTML.

> <snipped>

> I can't help you with your problem, but can you please stop sending the same
> thing in HTML. I access the net at work, so it doesn't cost me anything but
> time, others have to use the phone and postings like yours only help to push
> up their phone bill!

> AND DON'T EVER AGAIN POST THE SAME POSTING TO SEVEN GROUPS, CROSSPOST!!!!!

> Robert
> --
> Robert AH Prins
> prin...@williscorroon.com

Ok, Ok.... Sorry, I didn't know, it's only the second time I'm using forums.
But you say I make you waste your precious time, so why did you answer
(and  3 times!) ?!
 For your information, I post my request to 7 groups to get at least 1 good
answer
(not like yours!) and that's a french man  who answered me on a polish group!!!

Ps: I don't understand "crosspost", could you translate it in french ?

Re:Need help to improve a program in TP


Robert AH Prins a crit :

Quote
> This is not a pizza shop, if you post here, you must be prepared to pick up
> the solution here!

> > Here's the source code:
> <snipped>

> Aaaaagh!!!! Shudder! The same thing again in HTML.

> <snipped>

> I can't help you with your problem, but can you please stop sending the same
> thing in HTML. I access the net at work, so it doesn't cost me anything but
> time, others have to use the phone and postings like yours only help to push
> up their phone bill!

> AND DON'T EVER AGAIN POST THE SAME POSTING TO SEVEN GROUPS, CROSSPOST!!!!!

> Robert
> --
> Robert AH Prins
> prin...@williscorroon.com

Ok, Ok.... Sorry, I didn't know, it's only the second time I'm using
forums.
But you say I make you waste your precious time, so why did you
answer
(and  3 times!) ?!
 For your information, I post my request to 7 groups to get at least
1 good
answer
(not like yours!) and that's a french man  who answered me on a
polish group!!!

Ps: I don't understand "crosspost", could you translate it in french ?

Re:Need help to improve a program in TP


Robert AH Prins a crit :

Quote

> This is not a pizza shop, if you post here, you must be prepared to pick up
> the solution here!

> > Here's the source code:
> <snipped>

> Aaaaagh!!!! Shudder! The same thing again in HTML.

> <snipped>

> I can't help you with your problem, but can you please stop sending the same
> thing in HTML. I access the net at work, so it doesn't cost me anything but
> time, others have to use the phone and postings like yours only help to push
> up their phone bill!

> AND DON'T EVER AGAIN POST THE SAME POSTING TO SEVEN GROUPS, CROSSPOST!!!!!

> Robert
> --
> Robert AH Prins
> prin...@williscorroon.com

Ok, Ok.... Sorry, I didn't know, it's only the second time I'm using forums.
But you say I make you waste your precious time, so why did you answer
(and  3 times!) ?!
 For your information, I post my request to 7 groups to get at least 1 good
answer
(not like yours!) and that's a french man  who answered me on a polish group!!!

Ps: I don't understand "crosspost", could you translate it in french ?

Re:Need help to improve a program in TP


Quote
Gab wrote:
> Ps: I don't understand "crosspost", could you translate it in french ?

Cela signifie tout simplement que vous avez post le mme message dans plusieurs
newsgroups diffrents la fois. La netiquette veut que l'on attende d'abord de
recevoir une rponse dans un newsgroup. Si cela s'avre ne pas tre le cas, vous
pouvez tojours demander de l'aide dans un autre newsgroup plus tard.

Re:Need help to improve a program in TP


Quote
In article <36E00CD1.8ACD...@caramail.com>, Gab  <Ga...@caramail.com> wrote:

:Robert AH Prins a crit :
:> This is not a pizza shop, if you post here, you must be prepared to pick up
:> AND DON'T EVER AGAIN POST THE SAME POSTING TO SEVEN GROUPS, CROSSPOST!!!!!

:Ps: I don't understand "crosspost", could you translate it in french ?

Can't do that, since my French is not fluent enough, but I'll
explain the concept:

-From: ftp://garbo.uwasa.fi/pc/link/tsfaqn.zip Frequently Asked Questions
-Subject: Instructions about cross-posting

14. *****
 Q: What is cross-posting? How do I do it?

 A: As we all know, Usenet news have the newsgroups divided by the
topic areas. If you want your message to appear in more than one
newsgroup you can achieve this by cross-posting. If you look at the
header in the news you will notice the item Newsgroups:. Put the
names of the newsgroups in there separated by commas. Scan the
headers of almost any newsgroup, and you are bound to see how it is
done.
   The number one rule of cross-posting is that cross-posting should
never be used indiscriminately. If you feel that it is necessary to
cross-post, consider carefully your selection, and keep it down.
Avoid cross-posting to groups that are branches of the same
sub-hierarchy, that is don't cross-post to adjacent newsgroups.
   What goes for newsgroup selection in general, also applies to
cross-posting. Never cross-post to newsgroups which do not coincide
with your subject.
   There is one very important DON'T in cross-posting. Do not send
the same message separately to different newsgroups. Always use the
cross-posting facility of the news (Newsgroups:). If you repeat a
message separately in different newsgroups, the readers will have to
see your posting many times over, and will get annoyed. You have a
good chance of justifiably ending up flamed. Even worse, your site's
administrators are very likely to get angry complaints about your
multiple postings.
   I have heard that there are some newsreader programs that do not
allow editing the headers. I can only suggest contacting your system
manager or some other local guru about it. I have no further
information on this unusual dilemma since on most news programs
editing the headers is not a problem. Be careful, however, if you
edit the headers. Learn their exact requirements. If you make
mistakes, the posting may fail, and/or the followups to it by other
users may fail because of your editing errors. For example
 Newsgroups: comp.lang.pascal.borland,comp.os.msdos.programmer,
would result in an error in following up because of the trailing
comma.

 A2: Here are a couple of further tips when you have got the hang of
cross posting. As you can see there is a "Followup-To:" field in the
news header. Sometimes you might want to direct the replies only a
to single newsgroup even if you have cross posted the original. The
rationale here is to prevent the discussion from scattering to
several newsgroups. Please consider using this option whenever you
cross post.
   Some users put the word poster in there to redirect the potential
replies directly to them by email. The problem with this method is
that even if it should work, it is not guaranteed to do so. Some
system configurations and newsreaders do not handle this correctly.
For example I usually get a bounce if I reply to such a posting.
--------------------------------------------------------------------

   All the best, Timo

....................................................................
Prof. Timo Salmi   Co-moderator of news:comp.archives.msdos.announce
Moderating at ftp:// & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/>  ; FIN-65101,  Finland

Spam foiling in effect.  My email filter autoresponder will return a
required email password to users not yet in the privileges database.
Advice on spam foiling at http://www.uwasa.fi/~ts/info/spamfoil.html

Re:Need help to improve a program in TP


Gab [mailto:Ga...@caramail.com] decided to regale us with

Quote

>Ok, Ok.... Sorry, I didn't know, it's only the second time I'm using forums.
>But you say I make you waste your precious time, so why did you answer
>(and  3 times!) ?!

Because we did not know if you read all the groups that you posted to
(alot of posters do not when they post the same message to individual
and diverse groups).

Quote
> For your information, I post my request to 7 groups to get at least 1 good
>answer

You should always post to the group(s) that can _best_ answer your
question(s) not all those that have _pascal_ in their name you can
access from your News Server. If you post to *relevant* groups, you will
get good answers. Also, if you ask in the right newsgroups, if you get
an answer that is wrong, someone will be able to post an article to
correct the wrong information - this may not happen in ngs that are
outside the relevance of your question.

See ftp://garbo.uwasa.fi/pc/link/tsfaqn.zip and #14

<not relevant now to the groups posted to so reply set to poster>

--
Pedt

If smoking is so bad for you, how come it cures kippers?!

Re:Need help to improve a program in TP


In article <36E00C19.17916...@caramail.com>,
  Gab <Ga...@caramail.com> wrote:

Quote

> Robert AH Prins a crit :

> > This is not a pizza shop, if you post here, you must be prepared to pick up
> > the solution here!

> > > Here's the source code:
> > <snipped>

> > Aaaaagh!!!! Shudder! The same thing again in HTML.

> > <snipped>

> > I can't help you with your problem, but can you please stop sending the same
> > thing in HTML. I access the net at work, so it doesn't cost me anything but
> > time, others have to use the phone and postings like yours only help to push
> > up their phone bill!

> > AND DON'T EVER AGAIN POST THE SAME POSTING TO SEVEN GROUPS, CROSSPOST!!!!!

> > Robert
> > --
> > Robert AH Prins
> > prin...@williscorroon.com

> Ok, Ok.... Sorry, I didn't know, it's only the second time I'm using forums.

In that case, wouldn't it have been useful if you had first spend some time
looking into their functioning. You might have picked up the FAQ.

Quote
> But you say I make you waste your precious time, so why did you answer
> (and  3 times!) ?!

I cross-posted the answer to most of the groups you posted to, you have now
replied to each of them individually, generating even more extraneous bits in
need of bandwidth. (Really nice for people, who unlike me, have to pay for
Internet access)

Quote
>  For your information, I post my request to 7 groups to get at least 1 good
> answer
> (not like yours!) and that's a french man  who answered me on a polish

group!!!

Jeez, I am really stunned.

Quote
> Ps: I don't understand "crosspost", could you translate it in french ?

Robert
--
Robert AH Prins
prin...@williscorroon.com

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Re:Need help to improve a program in TP


Frdric <f...@home.ivm.de> schrieb in Nachricht
36E0166D.2A210...@home.ivm.de...///

Quote
Gab wrote:
> Ps: I don't understand "crosspost", could you translate it in french ?

Cela signifie tout simplement que vous avez post le mme message dans
plusieurs
newsgroups diffrents la fois. La netiquette veut que l'on attende d'abord
de
recevoir une rponse dans un newsgroup. Si cela s'avre ne pas tre le cas,
vous
pouvez tojours demander de l'aide dans un autre newsgroup plus tard.

this is a german newsgroup, so please answer in german or in english
so that everybody can understand you

Je crois, ce n'est pas un problem pour toi !

Re:Need help to improve a program in TP


Frank an der Heiden wrote:

Quote
> this is a german newsgroup, so please answer in german or in english
> so that everybody can understand you

> Je crois, ce n'est pas un problem pour toi !

Gab stated that he's not good at English. Why not do him the favor to
explain to him in his native language, as he requested?

Those who insist on English being the principal language even in an
*international* newsgroup (to which he also posted) should actually
not have any problems understanding the self-explanatory word "cross-
posting". :-/

Other Threads