Recent

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

marcus

  • New Member
  • *
  • Posts: 11
Re: Pascal Flight control simulation
« Reply #15 on: September 25, 2016, 07:14:39 pm »
@molly Thanks for your help, I can now fix the problem. I will come up with better programs in the future and thank you for your encouragement. :)

marcus

  • New Member
  • *
  • Posts: 11
Re: Pascal Flight control simulation
« Reply #16 on: September 25, 2016, 07:17:07 pm »
There is still a minor problem that the program is sometimes halted when the procedure arrival is run, I still can't find out the reason. I have tested many times and most of them work, just sometimes it doesn't.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Pascal Flight control simulation
« Reply #17 on: September 25, 2016, 07:27:01 pm »
There is still a minor problem that the program is sometimes halted when the procedure arrival is run, I still can't find out the reason. I have tested many times and most of them work, just sometimes it doesn't.
That is actually a though one as the error could depend on many variables. The biggest difficulty is that you use random values, so the chance of being able to repeat the exact same situation that generates the error is very slim.

As a first suggestion: Try to remove (comment out) the calls to randomize as that removing those will generate the same 'random' number sequence over and over again for each startup. Try and see if you are able to trigger the error. If you are able to then please show use exactly what you have given for input.

Other things to consider:
- try to see if it only happens with a odd or even number of flights entered
- check and see if it only happening when a certain number of gateways are already 'occupied'

Maybe someone else around has an idea. tbh i haven't looked at the rest of your code in depth yet as i'm a bit busy doing other things as well atm.

PS: in case you are sure the error is inside procedure arrival then log the value of every variable and print out something at every line so you can see what is happening exactly.
« Last Edit: September 25, 2016, 07:30:55 pm by molly »

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Pascal Flight control simulation
« Reply #18 on: September 26, 2016, 10:43:19 am »
PS: in case you are sure the error is inside procedure arrival then log the value of every variable and print out something at every line so you can see what is happening exactly.

Wouldn't it be beneficial to turn on [Debugging], set a break-point at the start of the 'Arrivals' and simply step through the code watching the pertinent variables?

Saves me a great deal of paper  :)
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

marcus

  • New Member
  • *
  • Posts: 11
Re: Pascal Flight control simulation
« Reply #19 on: September 26, 2016, 01:16:02 pm »
Thanks for all your help :) I gonna try different ways to figure out the problems.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Pascal Flight control simulation
« Reply #20 on: September 26, 2016, 01:19:33 pm »
PS: in case you are sure the error is inside procedure arrival then log the value of every variable and print out something at every line so you can see what is happening exactly.

Wouldn't it be beneficial to turn on [Debugging], set a break-point at the start of the 'Arrivals' and simply step through the code watching the pertinent variables?

Saves me a great deal of paper  :)
:P

Print out in my days just meant printing something to stdout. Basic, C etc. all use the command print. I should have written write out but in that case somebody would probably complain not having a pen or pencil around anymore these days  :D

Yes, using the debugger is normally the better approach but as said by TS, the 'halt' happens sporadically so first TS would need to figure out if the issue is reproducible. For that TS needs to have some values/situation that triggers the 'halt'. In my experience using some 'dumb' WriteLn's is often the easier approach for such situations, especially in case no clue what the code is doing. See also the first issue with regards to index out of bounds which showed that a simple writeLn tells that that specific part of code is/was doing something else that was not expected.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Pascal Flight control simulation
« Reply #21 on: September 26, 2016, 02:12:27 pm »
And as of sheer luck.  :D

Because J-G triggered me to have crt removed so i am able to log properly, the output shows:
Code: [Select]
> FlightControl
Please enter your choice (1. Departure 2. Arrival): 2
in arrival()
counter_arr = 0
Please wait for a moment.
t = 0
r = 2
t = 1
counter_arr = 1
start delay loop for 2 seconds
z = VA 865
assign random flight number for flight counter_arr#1
Assign airline from number r=2
t = 1
r = 1
t = 2
counter_arr = 2
start delay loop for 2 seconds
z = NP 622
assign random flight number for flight counter_arr#2
Assign airline from number r=1
t = 2
r = 4
t = 3
counter_arr = 3
start delay loop for 2 seconds
z = GX 372
assign random flight number for flight counter_arr#3
Assign airline from number r=4
t = 3
r = 5
t = 4
counter_arr = 4
start delay loop for 2 seconds
z = AI 633
assign random flight number for flight counter_arr#4
Assign airline from number r=5
t = 4
r = 2
t = 5
counter_arr = 5
start delay loop for 2 seconds
z = SL 529
assign random flight number for flight counter_arr#5
Assign airline from number r=2
t = 5
r = 5
t = 6
counter_arr = 6
start delay loop for 2 seconds
z = AF 263
assign random flight number for flight counter_arr#6
Assign airline from number r=5
t = 6
r = 1
t = 7
counter_arr = 7
start delay loop for 2 seconds
z = DW 219
assign random flight number for flight counter_arr#7
Assign airline from number r=1
t = 7
r = 2
t = 8
counter_arr = 8
start delay loop for 2 seconds
z = XG 925
assign random flight number for flight counter_arr#8
Assign airline from number r=2
t = 8
r = 1
t = 9
counter_arr = 9
start delay loop for 2 seconds
z = BH 304
assign random flight number for flight counter_arr#9
Assign airline from number r=1
t = 9
r = 3
t = 10
counter_arr = 10
start delay loop for 2 seconds
z = XN 623
assign random flight number for flight counter_arr#10
Assign airline from number r=3
t = 10
r = 4
t = 11
counter_arr = 11
start delay loop for 2 seconds
z = AS 271
assign random flight number for flight counter_arr#11
Assign airline from number r=4
t = 11
r = 2
t = 12
counter_arr = 12
start delay loop for 2 seconds
z = ET 518
assign random flight number for flight counter_arr#12
Assign airline from number r=2
t = 12
r = 4
t = 13
counter_arr = 13
start delay loop for 2 seconds
z = IP 330
assign random flight number for flight counter_arr#13
Assign airline from number r=4
t = 13
r = 3
t = 14
counter_arr = 14
start delay loop for 2 seconds
z = GD 822
assign random flight number for flight counter_arr#14
Assign airline from number r=3
t = 14
r = 3
t = 15
counter_arr = 15
start delay loop for 2 seconds
z = KW 244
assign random flight number for flight counter_arr#15
Assign airline from number r=3
t = 15
r = 4
t = 16
counter_arr = 16
start delay loop for 2 seconds
z = NP 765
assign random flight number for flight counter_arr#16
Assign airline from number r=4
t = 16
r = 4
t = 17
counter_arr = 17
start delay loop for 2 seconds
z = DA 872
assign random flight number for flight counter_arr#17
Assign airline from number r=4
t = 17
r = 5
t = 18
counter_arr = 18
start delay loop for 2 seconds
z = VN 674
assign random flight number for flight counter_arr#18
Assign airline from number r=5
t = 18
r = 4
t = 19
counter_arr = 19
start delay loop for 2 seconds
z = KO 882
assign random flight number for flight counter_arr#19
Assign airline from number r=4
t = 19
r = 4
t = 20
counter_arr = 20
start delay loop for 2 seconds
z = JI 696
assign random flight number for flight counter_arr#20
Assign airline from number r=4
t = 20
r = 2
t = 21
counter_arr = 21
start delay loop for 2 seconds
z = NP 915
assign random flight number for flight counter_arr#21
Assign airline from number r=2
t = 21
r = 1
t = 22
counter_arr = 22
start delay loop for 2 seconds
z = UX 327
assign random flight number for flight counter_arr#22
Assign airline from number r=1
t = 22
r = 2
t = 23
counter_arr = 23
start delay loop for 2 seconds
z = BD 222
assign random flight number for flight counter_arr#23
Assign airline from number r=2
t = 23
r = 2
t = 24
counter_arr = 24
start delay loop for 2 seconds
z = GH 135
assign random flight number for flight counter_arr#24
Assign airline from number r=2
t = 24
r = 2
t = 25
counter_arr = 25
start delay loop for 2 seconds
z = SF 648
assign random flight number for flight counter_arr#25
Assign airline from number r=2
t = 25
r = 1
t = 26
counter_arr = 26
start delay loop for 2 seconds
z = MU 126
assign random flight number for flight counter_arr#26
Assign airline from number r=1
t = 26
r = 3
t = 27
counter_arr = 27
start delay loop for 2 seconds
z = RJ 697
assign random flight number for flight counter_arr#27
Assign airline from number r=3
t = 27
r = 4
t = 28
counter_arr = 28
start delay loop for 2 seconds
z = ZF 331
assign random flight number for flight counter_arr#28
Assign airline from number r=4
t = 28
r = 3
t = 29
counter_arr = 29
start delay loop for 2 seconds
z = FP 434
assign random flight number for flight counter_arr#29
Assign airline from number r=3
t = 29
r = 1
t = 30
counter_arr = 30
start delay loop for 2 seconds
z = SJ 361
assign random flight number for flight counter_arr#30
Assign airline from number r=1
t = 30
r = 1
t = 31
counter_arr = 31
start delay loop for 2 seconds
z = XK 840
assign random flight number for flight counter_arr#31
Assign airline from number r=1
t = 31
r = 4
t = 32
counter_arr = 32
start delay loop for 2 seconds
z = JE 002
assign random flight number for flight counter_arr#32
Assign airline from number r=4
t = 32
r = 5
t = 33
counter_arr = 33
start delay loop for 2 seconds
z = FL 778
assign random flight number for flight counter_arr#33
Assign airline from number r=5
t = 33
r = 5
t = 34
counter_arr = 34
start delay loop for 2 seconds
z = VO 425
assign random flight number for flight counter_arr#34
Assign airline from number r=5
t = 34
r = 5
t = 35
counter_arr = 35
start delay loop for 2 seconds
z = LG 235
assign random flight number for flight counter_arr#35
Assign airline from number r=5
t = 35
r = 2
t = 36
counter_arr = 36
start delay loop for 2 seconds
z = FB 014
assign random flight number for flight counter_arr#36
Assign airline from number r=2
t = 36
r = 2
t = 37
counter_arr = 37
start delay loop for 2 seconds
z = IW 243
assign random flight number for flight counter_arr#37
Assign airline from number r=2
t = 37
r = 5
t = 38
counter_arr = 38
start delay loop for 2 seconds
z = IR 933
assign random flight number for flight counter_arr#38
Assign airline from number r=5
t = 38
r = 5
t = 39
counter_arr = 39
start delay loop for 2 seconds
z = NI 360
assign random flight number for flight counter_arr#39
Assign airline from number r=5
t = 39
r = 1
t = 40
counter_arr = 40
start delay loop for 2 seconds
z = NK 573
assign random flight number for flight counter_arr#40
Assign airline from number r=1
t = 40
r = 4
t = 41
counter_arr = 41
start delay loop for 2 seconds
z = ET 771
assign random flight number for flight counter_arr#41
Assign airline from number r=4
t = 41
r = 3
t = 42
counter_arr = 42
start delay loop for 2 seconds
z = ZQ 773
assign random flight number for flight counter_arr#42
Assign airline from number r=3
t = 42
r = 5
t = 43
counter_arr = 43
start delay loop for 2 seconds
z = UB 018
assign random flight number for flight counter_arr#43
Assign airline from number r=5
t = 43
r = 4
t = 44
counter_arr = 44
start delay loop for 2 seconds
z = YD 675
assign random flight number for flight counter_arr#44
Assign airline from number r=4
t = 44
r = 3
t = 45
counter_arr = 45
start delay loop for 2 seconds
z = FU 570
assign random flight number for flight counter_arr#45
Assign airline from number r=3
t = 45
r = 2
t = 46
counter_arr = 46
start delay loop for 2 seconds
z = PX 269
assign random flight number for flight counter_arr#46
Assign airline from number r=2
t = 46
r = 3
t = 47
counter_arr = 47
start delay loop for 2 seconds
z = SO 980
assign random flight number for flight counter_arr#47
Assign airline from number r=3
t = 47
r = 4
t = 48
counter_arr = 48
start delay loop for 2 seconds
z = TT 635
assign random flight number for flight counter_arr#48
Assign airline from number r=4
t = 48
r = 5
t = 49
counter_arr = 49
start delay loop for 2 seconds
z = ZN 630
assign random flight number for flight counter_arr#49
Assign airline from number r=5
t = 49
r = 1
t = 50
counter_arr = 50
start delay loop for 2 seconds
z = OV 225
assign random flight number for flight counter_arr#50
Assign airline from number r=1
t = 50
r = 4
t = 51
counter_arr = 51
start delay loop for 2 seconds
^C
>

Now to TS: Was your intention to loop over this more then 50 times ? If answered yes, then explain why. If answered no, then try to figure out why it does.

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Pascal Flight control simulation
« Reply #22 on: September 26, 2016, 02:49:41 pm »
Print out in my days just meant printing something to stdout. Basic, C etc. all use the command print. I should have written write out but in that case somebody would probably complain not having a pen or pencil around anymore these days  :D

Ah.. I've never looked seriously at C and my days of BASIC ended when I found Pascal for the Nascom in 1981, though I did do a year of COBOL which also uses 'Print' IIRC.

I do tend to take things literally and I might well have picked up on 'write out' as well  :D   - - -  though I do have a desk littered with paper and (currently) 6 pencils!

Pleased I was able to trigger a thought.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

marcus

  • New Member
  • *
  • Posts: 11
Re: Pascal Flight control simulation
« Reply #23 on: September 26, 2016, 02:56:44 pm »
But why the procedure will loop over this more then 50 times ? I have written "until t= random(3)+3" at the end of that procedure to limit the number of loops within 3 to 5.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Pascal Flight control simulation
« Reply #24 on: September 26, 2016, 03:04:01 pm »
But why the procedure will loop over this more then 50 times ? I have written "until t= random(3)+3" at the end of that procedure to limit the number of loops within 3 to 5.
Are you absolutely positively sure about that conclusion ?

Let's put this to the test and start a new empty project.
Code: [Select]
Program testloop;

procedure DoLoop;
var
  AValue, LimitValue : integer;
begin
  AValue := 0;

  Repeat
    Inc(AValue);
    LimitValue := random(3)+3;
   WriteLn('AValue =', AValue, ' LimitValue = ', LimitValue);
  Until AValue = LimitValue;

end;

begin
  Randomize;
  DoLoop;
end.

Try to run that and see what happens. If you do not 'trigger' the error, run it a couple of more times.

edit:
In case you do not trigger the error then the output shown is most interesting for your situation as well.

Code: [Select]
AValue =1 LimitValue = 5
AValue =2 LimitValue = 5
AValue =3 LimitValue = 4
AValue =4 LimitValue = 4

Do you see/understand what is happening there ?

edit2:
Phew!, it took me some time to trigger when things go wrong using random generated values, but in the end it was able to :-)

Code: [Select]
AValue =1 LimitValue = 5
AValue =2 LimitValue = 3
AValue =3 LimitValue = 4
AValue =4 LimitValue = 3
AValue =5 LimitValue = 4
AValue =6 LimitValue = 4
AValue =7 LimitValue = 3
AValue =8 LimitValue = 4
AValue =9 LimitValue = 4
AValue =10 LimitValue = 5
etc.
etc.
The rest of the output is of no importance as the 'bug' was already encountered.
« Last Edit: September 26, 2016, 03:31:59 pm by molly »

marcus

  • New Member
  • *
  • Posts: 11
Re: Pascal Flight control simulation
« Reply #25 on: September 26, 2016, 04:20:06 pm »
I get what you mean and I think I have fixed it. Many thanks @ molly :D
« Last Edit: September 26, 2016, 04:27:23 pm by marcus »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Pascal Flight control simulation
« Reply #26 on: September 26, 2016, 04:28:28 pm »
I get what you mean but why is there such a problem?
Because on each iteration, when you use the until statement, you are calculating a fresh new condition (using random function) to check against and that determines whether the loop should end or not.

As the latest log output shows, it can happen that you do the calculation that results in an answer that completely misses the window/range for which you had designed it for.

Why do you need to calculate your 'endvalue' on each and every iteration ? Isn't just doing that calculation once (outside) the loop a more logical approach ? You could even replace your repeat with a for loop in that case.

The answer to that question is interesting, as i am unable to see a clear explanation of why you would want to do as you have currently implemented. It absolutely makes no sense to/for me (but please do not take that personally, it might just be my shortsighted vision on your code).
« Last Edit: September 26, 2016, 04:31:07 pm by molly »

marcus

  • New Member
  • *
  • Posts: 11
Re: Pascal Flight control simulation
« Reply #27 on: September 26, 2016, 04:54:29 pm »
I got the program fixed without halt anymore, I really appreciate and thank you for your help. There is still a long way for me in the world of programming, it is obvious that there will be many challenge waiting for me. :) :)

 

TinyPortal © 2005-2018