Recent

Author Topic: Pascal Flight control simulation  (Read 9597 times)

marcus

  • New Member
  • *
  • Posts: 11
Pascal Flight control simulation
« on: September 25, 2016, 06:44:14 am »
The program allows users to choose from departure and arrival with recursion.However it shows exit code 201 when only one flight is entered during departure.What is the problem?

The program sometimes also halts during arrival (generating random flight number and airline for the planes), is there too many loops or what?
Here are the codes.

Code: Pascal  [Select][+][-]
  1.  
  2. program flight_control;
  3. uses crt;
  4. type
  5.   flighttype = record
  6.                  number: string[8];
  7.                  line: string;
  8.                  gate: char;
  9.                  runway: integer
  10.                  end;
  11. var
  12.   plane: array[1..50] of flighttype;
  13.   num,ans: integer;
  14.   ans_strg: string;
  15.   code_main: integer;
  16.  
  17. procedure departure(var count: integer);
  18. var
  19.   flight: string[8];
  20.   airline: string;
  21.   a,b,c,d,code :integer;
  22.   check: boolean;
  23.   e,f,ans: char;
  24. begin
  25.   count:=0;
  26.   repeat
  27.   repeat
  28.   check:= false;
  29.   write('Please enter the flight in capital letter.(With space bar between characters and numbers): ');
  30.   readln(flight);
  31.   a:= ord(flight[1]);
  32.   b:= ord(flight[2]);
  33.   c:= ord(flight[3]);
  34.   val(copy(flight,4,4),d,code);
  35.   if ((a>=65) and (a<=90)) and ((b>=65) and (b<=90)) and (c=32) and (code=0) and (length(flight)>=6) and (length(flight)<=7)
  36.   then check:= true;
  37.   if check= false
  38.   then writeln('Please enter again.')
  39.   until check= true;
  40.   write('Please enter the airline: ');
  41.   readln(airline);
  42.   count:= count+1;
  43.   with plane[count] do
  44.   begin
  45.   number:= flight;
  46.   line:= airline;
  47.   gate:=' '
  48.   end;
  49.   repeat
  50.   write('Input another flight?(Y/N): ');
  51.   readln(ans)
  52.   until ans in['y', 'Y', 'n', 'N']
  53.   until ans in ['N', 'n']
  54.   end;
  55.  
  56. procedure arrival(var counter_arr: integer);
  57. var
  58.   time,{x,y,}t,r: integer;
  59.   z: string[8];
  60. begin
  61.   randomize;
  62.   t:= 0;
  63.   writeln('Please wait for a moment.');
  64.   repeat
  65.   r:= random(5)+1;
  66.   t:= t+1;
  67.   counter_arr:= counter_arr+1;
  68.   time:=2;
  69.   while time>=0 do
  70.   begin
  71.     time:= time-1;
  72.     delay(1000)
  73.   end;
  74.   z:='';
  75.   {for x:= 1 to 2 do
  76.   z:= z+chr(random(26)+65);
  77.   z:= z+' ';
  78.   for y:= 1 to (random(2)+3) do
  79.   z:= z+chr(random(10)+48);}
  80.   z:=z+chr(random(26)+65)+chr(random(26)+65)+' '+chr(random(10)+48)+chr(random(10)+48)+chr(random(10)+48);
  81.   with plane[counter_arr] do
  82.   begin
  83.     number:= z;
  84.     case r of
  85.     1: line:= 'AA Pacific Company';
  86.     2: line:= 'BB Pacific Company';
  87.     3: line:= 'CC Pacific Company';
  88.     4: line:= 'DD Pacific Company';
  89.     5: line:= 'EE Pacific Company'
  90.     end
  91.   end
  92.   until t= random(3)+3
  93. end;
  94.  
  95. procedure display(counter: integer);
  96. var
  97.   p: integer;
  98. begin
  99.   p:=0;
  100.   writeln;
  101.   writeln('Number   ','Flight   ','Airline');
  102.   writeln;
  103.   repeat
  104.   p:= p+1;
  105.   with plane[p] do
  106.   writeln(p:3,'  ',number:10,'   ',line)
  107.   until p= counter;
  108.   writeln
  109. end;
  110.  
  111.  
  112. procedure gate(counter,ans_gate: integer);
  113. var
  114.   m,n,p,j: integer;
  115.   time_dep: integer;
  116. begin
  117.   randomize;
  118.   for p:= 1 to counter do
  119.   with plane[p] do
  120.   gate:=chr(random(26)+65);
  121.   p:=0;
  122.   repeat
  123.   p:= p+1;
  124.   for j:= p+1 to counter do
  125.   if plane[p].gate = plane[j].gate
  126.   then repeat
  127.        plane[j].gate:= chr(random(26)+65)
  128.        until plane[p].gate <> plane[j].gate
  129.   until p= counter-1;
  130.   for p:= 1 to counter do
  131.   with plane[p] do
  132.   begin
  133.   time_dep:=2;
  134.   while time_dep>=0 do
  135.   begin
  136.     time_dep:= time_dep-1;
  137.     delay(1000)
  138.   end;
  139.   writeln('Gate ',gate,' is assigned for flight ',number,'.');
  140.   writeln
  141.   end;
  142.   for p:= 1 to counter do
  143.   with plane[p] do
  144.   begin
  145.   time_dep:=5;
  146.   while time_dep>=0 do
  147.   begin
  148.     time_dep:= time_dep-1;
  149.     delay(1000)
  150.   end;
  151.   if ans_gate= 1
  152.   then begin
  153.          writeln('Gate ',gate,' is now closing.');
  154.          writeln;
  155.          time_dep:= random(3)+3;
  156.          while time_dep>=0 do
  157.          begin
  158.            time_dep:= time_dep-1;
  159.            delay(1000)
  160.          end;
  161.          writeln('Flight ',number,' asks for runway.');
  162.          writeln
  163.        end
  164.   else begin
  165.          writeln('Passengers from flight ',number,' will reach Arrival Hall ',chr(random(2)+65),'.');
  166.          writeln
  167.        end
  168.   end
  169. end;
  170.  
  171. procedure runway(var counter_dep: integer; ans_run: integer);
  172. var
  173.   time_dep: integer;
  174.   p,q: integer;
  175. begin
  176.   p:=0;
  177.   repeat
  178.   p:= p+1;
  179.   plane[p].runway:= random(2)+1;
  180.   time_dep:=3;
  181.   while time_dep>=0 do
  182.   begin
  183.     time_dep:= time_dep-1;
  184.     delay(1000)
  185.   end;
  186.   writeln('Runway ', plane[p].runway,' is assigned for flight ',plane[p].number,'.');
  187.   writeln;
  188.   time_dep:= 2;
  189.   while time_dep>= 0 do
  190.   begin
  191.     time_dep:= time_dep-1;
  192.     delay(1000)
  193.   end;
  194.   for q:= p+1 to counter_dep do
  195.   begin
  196.   writeln('The runways are not yet available for flight ',plane[q].number,'.');
  197.   writeln
  198.   end;
  199.   time_dep:=random(3)+3;
  200.   while time_dep>= 0 do
  201.   begin
  202.     time_dep:= time_dep-1;
  203.     delay(1000)
  204.   end;
  205.  
  206.   if ans_run= 1
  207.   then with plane[p] do
  208.        begin
  209.          writeln('Flight ',number,' has taken off.');
  210.          writeln
  211.        end;
  212.   if ans_run= 2
  213.   then with plane[p] do
  214.        begin
  215.          writeln('Flight ',number,' has landed.');
  216.          writeln
  217.        end
  218.   until p= counter_dep
  219. end;
  220.  
  221. procedure clear(var counter: integer);
  222. var
  223.   p: integer;
  224. begin
  225.   for p:= 1 to counter do
  226.   with plane[p] do
  227.   begin
  228.     number:='';
  229.     line:='';
  230.     gate:=' ';
  231.     runway:=0
  232.   end;
  233.   counter:= 0
  234. end;
  235.  
  236. procedure one(num_one,ans_one: integer);
  237. begin
  238.   departure(num_one);
  239.   display(num_one);
  240.   gate(num_one,ans_one);
  241.   runway(num_one,ans_one);
  242.   clear(num_one)
  243. end;
  244.  
  245. procedure two(num_two,ans_two: integer);
  246. begin
  247.   arrival(num_two);
  248.   display(num_two);
  249.   runway(num_two,ans_two);
  250.   gate(num_two,ans_two);
  251.   clear(num_two)
  252. end;
  253.  
  254. procedure repeating;
  255. var
  256.   ans_a: string;
  257.   ans_b: integer;
  258.   code_one: integer;
  259. begin
  260.   repeat
  261.   write('Please enter your choice (1. Departure 2. Arrival 3. Exit): ');
  262.   readln(ans_a);
  263.   val(ans_a,ans_b,code_one)
  264.   until (ans_b= 1) or (ans_b= 2)  or (ans_b= 3) and (code_one= 0);
  265.   if ans_b=1
  266.   then begin
  267.          one(num,1);
  268.          repeating
  269.        end;
  270.   if ans_b=2
  271.   then begin
  272.          two(num,2);
  273.          repeating
  274.        end
  275. end;
  276.  
  277. begin
  278.   repeat
  279.   write('Please enter your choice (1. Departure 2. Arrival): ');
  280.   readln(ans_strg);
  281.   val(ans_strg,ans,code_main)
  282.   until (ans= 1) or (ans= 2) and (code_main= 0);
  283.   if ans= 1
  284.   then one(num,ans)
  285.   else two(num,ans);
  286.   repeating;
  287.   readln
  288. end.
  289.  

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: Pascal Flight control simulation
« Reply #1 on: September 25, 2016, 08:53:45 am »
There are several issues with your code.
201 means a range check error.  I suspect that at some point you generate an index to the plane array of zero or an index that is above 50. It is usually better to define that array  as [0..49].
And clean up the hints about variables not initialized. That can also be a cause.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

marcus

  • New Member
  • *
  • Posts: 11
Re: Pascal Flight control simulation
« Reply #2 on: September 25, 2016, 10:22:26 am »
But after changing the range of the array to [0..49] range check error still occurs.Is there other source of errors? Could someone also teach me how to clean up those variables in order not to make the program halts during running?

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: Pascal Flight control simulation
« Reply #3 on: September 25, 2016, 02:13:21 pm »
But after changing the range of the array to [0..49] range check error still occurs.Is there other source of errors? Could someone also teach me how to clean up those variables in order not to make the program halts during running?


It is not just the range of the array: you also need to change the rest of your code with the array 0..49 in mind.
Another overflow is in the way you use random: You always add 1 but that is also wrong: ib general given a set of 256 chars they run from 0 to 255 .
There are too many things wrong with that code. Did you write it? Then I see if I can show you step by step.
I also would appreciate it if you add some valid expected input. That is absolutely not clear and also needs a lot of work.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

marcus

  • New Member
  • *
  • Posts: 11
Re: Pascal Flight control simulation
« Reply #4 on: September 25, 2016, 03:12:04 pm »
Yes, the code is written by me.I still not too understand your points concerning the range of the array and the random function.Could you please show me step by step if possible? I have learnt pascal for a short period of time therefore not too familiar with it.
Here are some examples of expected valid input:
For flight eg: AA 500 or BB 9999
For airline eg: British Airways or Bangkok Airways


wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Pascal Flight control simulation
« Reply #5 on: September 25, 2016, 03:46:12 pm »
Another overflow is in the way you use random: You always add 1 but that is also wrong: ib general given a set of 256 chars they run from 0 to 255 .
I think Marcus' code is correct here. In procedure "Arrival", for example, he tries to create a random 8-character-string like this
Code: Pascal  [Select][+][-]
  1. var
  2.   z: String[8];
  3. ...
  4.   z:=z+chr(random(26)+65)+chr(random(26)+65)+' '+chr(random(10)+48)+chr(random(10)+48)+chr(random(10)+48);
There are 26 characters in the alphabet, the first one, the 'A' has code 65. The function random(26) creates random integer values starting at 0 and ending at 25. Therefore, the highest character generated is the one with code 65+25 = 90 --> 'Z'.

Marcus, why don't use the debugger. Suppose you want to check the lines in the Arrival procedure. Set a breakpoint at the line which you want to inspect by clicking in the gray gutter area (left of the line number) at the height of this line. A red circle appears to indicate that the program will stop here. Run the program, as expected it will stop at the breakpoint. Move the mouse above a variable (no click) and an popup window will show you the current value of the variable.  Press F8 to go the next line, or F7 to step into a subprogram (but this works only if the source can be found), or F9 to continue the program - it will run normally or stop at the next breakpoint. In this way you can step through the program line by line, and you can check every variable. There are other techniques. Look at menu "View" / "Debug Windows". You will find out easily once you understood the idea.

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: Pascal Flight control simulation
« Reply #6 on: September 25, 2016, 04:52:32 pm »
r:= random(5)+1; ?

Around line 139 change your original  code to:
Code: Pascal  [Select][+][-]
  1.   Repeat
  2.     p := p+1;  // p was zero, but you already increased it.
  3.     For j:= p To counter Do
  4.       If plane[p].gate = plane[j].gate
  5.  

You were increasing p two times. That's what the debugger said.
Why didn't you check?
« Last Edit: September 25, 2016, 05:04:16 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Pascal Flight control simulation
« Reply #7 on: September 25, 2016, 04:57:50 pm »
Although i agreee with running things through the debugger, this one was actually quite quick to catch with range-checking turned on and line numbers turned on (-gl switch).

Code: Pascal  [Select][+][-]
  1. procedure gate(counter,ans_gate: integer);
  2. var
  3.   m,n,p,j: integer;
  4.   time_dep: integer;
  5. begin
  6.   randomize;
  7.   for p:= 1 to counter do
  8.   with plane[p] do
  9.   gate:=chr(random(26)+65);
  10.   p:=0;
  11.   repeat
  12.   p:= p+1;
  13.   for j:= p+1 to counter do
  14.   if plane[p].gate = plane[j].gate
  15.   then repeat
  16.        plane[j].gate:= chr(random(26)+65)
  17.        until plane[p].gate <> plane[j].gate
  18.   until p= counter-1;
  19.   for p:= 1 to counter do
  20.   with plane[p] do
  21.   begin
  22.   time_dep:=2;
  23.   while time_dep>=0 do
  24.   begin
  25.     time_dep:= time_dep-1;
  26.     delay(1000)
  27.   end;
  28.   writeln('Gate ',gate,' is assigned for flight ',number,'.');
  29.   writeln
  30.   end;
  31.   for p:= 1 to counter do
  32.   with plane[p] do
  33.   begin
  34.   time_dep:=5;
  35.   while time_dep>=0 do
  36.   begin
  37.     time_dep:= time_dep-1;
  38.     delay(1000)
  39.   end;
  40.   if ans_gate= 1
  41.   then begin
  42.          writeln('Gate ',gate,' is now closing.');
  43.          writeln;
  44.          time_dep:= random(3)+3;
  45.          while time_dep>=0 do
  46.          begin
  47.            time_dep:= time_dep-1;
  48.            delay(1000)
  49.          end;
  50.          writeln('Flight ',number,' asks for runway.');
  51.          writeln
  52.        end
  53.   else begin
  54.          writeln('Passengers from flight ',number,' will reach Arrival Hall ',chr(random(2)+65),'.');
  55.          writeln
  56.        end
  57.   end
  58. end;
  59.  

Take a (good) look at the highlighted lines.

I make it more concrete for you. Between line numbers 12 and 13, add the following line:
Code: [Select]
    WriteLn(' p = ', p, ' counter = ', counter);
And see what happens... for me that does not make sense, or am i mistaken  ?

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: Pascal Flight control simulation
« Reply #8 on: September 25, 2016, 05:05:01 pm »
@molly same conclusion. posts crossed
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Pascal Flight control simulation
« Reply #9 on: September 25, 2016, 05:06:21 pm »
r:= random(5)+1; ?
What's wrong with it? Yes, r := random(5) and "case r of 0...4" would be more convenient and simpler than "r := random(5)+1" and "case r of 1..5", but I don't see a bug in the logics.

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: Pascal Flight control simulation
« Reply #10 on: September 25, 2016, 05:10:38 pm »
doesn't sound right to generate an index. Bu we found the bug somewhere else.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Pascal Flight control simulation
« Reply #11 on: September 25, 2016, 05:24:15 pm »
@Thaddy:
Sorry, it was not my intention to rob you from the answer.

Although not too complicated code, it is actually a very nice program for a beginner.

Reminds me a bit of one of those shovel-ware games delivered with some windows version were you had to land and departure the planes on the correct runways, guide them to the correct gate, clean them up and keep track of circling waiting flights in the air :-)

Keep on going marcus !  :)


PS: actually there is a small caveat when only changing p.

I believe this is what TS had in mind (following original code here).:
Code: Pascal  [Select][+][-]
  1. ...
  2.   p := 0;
  3.  
  4.   repeat
  5.     p:= p + 1;
  6.     WriteLn(' p = ', p, ' counter = ', counter);
  7.     for j := p+1 to counter do
  8.       if plane[p].gate = plane[j].gate then
  9.       repeat
  10.         plane[j].gate:= chr(random(26)+65)
  11.       until plane[p].gate <> plane[j].gate;
  12.   until (p = counter);
  13. ...
  14.  
Seems a check against other gates is only necessary when there are more than 1 planes. Hence the somewhat strange + 1 for the start counter of the for-loop. But not taking that into account for the until (original reads : p=counter-1) check, lets the outer repeat loop overrun.
« Last Edit: September 25, 2016, 06:43:30 pm by molly »

marcus

  • New Member
  • *
  • Posts: 11
Re: Pascal Flight control simulation
« Reply #12 on: September 25, 2016, 06:45:47 pm »
r:= random(5)+1; ?

Around line 139 change your original  code to:
Code: Pascal  [Select][+][-]
  1.   Repeat
  2.     p := p+1;  // p was zero, but you already increased it.
  3.     For j:= p To counter Do
  4.       If plane[p].gate = plane[j].gate
  5.  

You were increasing p two times. That's what the debugger said.
Why didn't you check?
I am checking whether the gates assigned for each flight will be the same,therefore assigning the value of p+1 to j.
Eg. for the first flight,i.e. p=1 I am checking whether the gates for flights 2 to counter will be the same as that of flight one. Therefore I don't know whether this an error.
« Last Edit: September 25, 2016, 06:47:24 pm by marcus »

marcus

  • New Member
  • *
  • Posts: 11
Re: Pascal Flight control simulation
« Reply #13 on: September 25, 2016, 06:55:55 pm »
When the program has finished the input of the first flight with procedure departure, it gives the message exit code 201(range check error) if n is entered for the variable ans. I am not sure about what's happening here because if information of two flights or above is inputted,the program can run smoothly. Sorry for asking so many questions.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Pascal Flight control simulation
« Reply #14 on: September 25, 2016, 07:09:27 pm »
I am checking whether the gates assigned for each flight will be the same,therefore assigning the value of p+1 to j.
Eg. for the first flight,i.e. p=1 I am checking whether the gates for flights 2 to counter will be the same as that of flight one. Therefore I don't know whether this an error.
As i thought. The only thing i do not know if my fix is working for the rest of your code as intended.

In that case either
a) Change the until check for the outer repeat as shown or
b) add a proper check (if then) for that case where there is only one flight available (to be able to skip the gate-checking).

When the program has finished the input of the first flight with procedure departure, it gives the message exit code 201(range check error) if n is entered for the variable ans. I am not sure about what's happening here because if information of two flights or above is inputted,the program can run smoothly. Sorry for asking so many questions.
Yes. The error is clear, as is the reason.

Just put the writeln in between and you see that p is counting up till the integer is maxed out, and using it as an index will give you the 201 error.

Quote
201 Range check error
If you compiled your program with range checking on, then you can get this error in the following cases:
1) An array was accessed with an index outside its declared range.
2) Trying to assign a value to a variable outside its range (for instance an enumerated type).

Asking question is not a problem but, it is your code so we assume that you know what is the intention of your code . If you do not even know that then we are doomed :D

 

TinyPortal © 2005-2018