Re: Help to make a better shortest route program



Rod schrieb:
Hello,

I am in 10th grade and I am teaching myself programming because we are
not taught from our schools. I have an old (turbo pascal 7.0) book
from my father and I use free pascal for linux.

A good idea. I think Pascal is still worth learning.

We had a competition for writing a program to find the shortest trip
between two cities. I wrote the program and it worked so I sent the
program. It was not one chosen for winning because they said there
were other programs sent which executed faster. I would like to know
what I could do to make the program better. I will enter again next
year and I want to learn more.

Good luck!

We could write in pascal or C++ or C. If I wrote in another language
would it be faster? Please tell me what you think

It is not the language which makes a program fast. If your program is
slow, you have to look at the algorithm and find out why it takes so
long to execute. In what way do costs (execution speed, memory
consumption) raise if input data gets larger?

So: generally look at your algorithm and make it better. Then review the
algorithm again. And again. And again.

Here is the code I wrote and sent in.
----------------------------------

Program apollon;
{(Free Pascal 1.0.10 for i386 Linux) }
Uses
dos, crt;

Const
FSI = 'apollon.in';
FSO = 'apollon.out';

LIL = 1;
UIL = 210925; { THE PROBLEM GIVES THIS }

What are FSI, FSO, LIL and UIL? Better give longer, more readable names
to constants.


Type

routetype = Record
FromCity : integer;
ToCity : integer;
Distance : integer;
End;

citytype = Record
Count : integer;
Shortest : integer;
end;


IndexType = LIL .. UIL;

IndexType... Well, a wonderful name. Could mean anything!


Var
i : integer; {index}
j : integer; {a second index}
c : integer; {number of cities}
r : integer; {number of routes between cities}
it : text; {input file}
ot : text; {output file}
arin : array [IndexType] Of routetype;
StartCity : integer;
StopCity : integer;
cc : integer; {city count index}
arci : array [IndexType] Of citytype; {array to count routes from
each city}
chk : integer; {chk for early stop}
shortest : integer;
next : integer;
Dist : integer;
sum, shortestpath, nextarin : integer; {for Crawl}
Biggy: integer; {used by Procedure Out}
mext, lext : integer;
addit : integer;
y, z : integer;

Huh!
Way too many global variables!!

And for naming again:
If you declare i: Integer - then it is clear that this is meant as index.
For c: better name it NumberOfCities: Integer - then you can get rid of
the comment.

If a variable is used only within a procedure or function then it should
not be global. For example cc is only used in CounttheCityRoads - why
don't you declare it within that procedure? Hide as much information
from the outside as possible.

None of your procedures has a parameter. It's not that procedures
without parameters don't work - but with parameters they are often more
powerful because they can be applied to different things.

Wolf
.