Recent

Author Topic: Example Code Question  (Read 3831 times)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Example Code Question
« Reply #15 on: April 22, 2019, 09:53:50 pm »
The original question was whether there was some place were one could add example code, etc. so that Lazarus/FPC users could find it among its friends by other programmers.

And the answer is that there is one place for that: The Lazarus Code and Component Repository in SourceForge and the Lazarus-ccr GitHub organization

I'm surprised none thought of that.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Example Code Question
« Reply #16 on: April 22, 2019, 10:28:09 pm »
I'll Check it out - Thanks
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: Example Code Question
« Reply #17 on: April 24, 2019, 01:29:58 pm »

JLWEST
What do use haversines for?
(I used them in the early part of my working life)
Retired now.

The bog standard Quicksort for integer is very fast for repeats of your type (3 to 20 integers say)
Sorry, I am not a Lazarus user.
Code: Pascal  [Select][+][-]
  1.  
  2. Program qsort ;
  3.  {$rangeChecks on}
  4. Uses
  5. SysUtils, DateUtils ; {for timer}
  6.  
  7. Type
  8.   IntegerArray = Array Of Integer;
  9.  
  10. Procedure QuickSort(Var arr:IntegerArray;Start:Integer;Finish:Integer);
  11. Var
  12.   i,j: Integer;
  13.   x,temp: Integer;
  14. Begin
  15.   i := Start ;
  16.   j := finish;
  17.   x := arr[(Start+finish) Div 2];
  18.   While I <= J Do
  19.     Begin
  20.       While arr[I] < x Do
  21.         i+= 1;
  22.       While arr[J] > x Do
  23.         j-= 1;
  24.       If I<=J Then
  25.         Begin
  26.           temp := arr[j];
  27.           arr[j] := arr[i];
  28.           arr[i] := temp;
  29.           I+= 1;
  30.           J-= 1;
  31.         End;{if}
  32.     End;{while i<=j}
  33.   If J > Start Then QuickSort(arr,Start,J);
  34.   If I < Finish Then QuickSort(arr,I,Finish);
  35. End;{quicksort}
  36.  
  37. Function range(first:Integer;last:Integer): Integer;inline;
  38. Begin
  39.   Exit( Trunc( Random()*(last-first+1)) + first )
  40. End; {range}
  41.  
  42. Procedure addone(Var a:IntegerArray);
  43. Begin
  44.   setlength(a,high(a)+2);
  45.   a[high(a)] := range(100,200);
  46. End;{addone}
  47.  
  48. Procedure setup(Var a:IntegerArray);
  49. Var
  50.   i: Integer;
  51. Begin
  52.   For i:=0 To high(a) Do
  53.     a[i] := range(100,200);
  54. End;{setup}
  55.  
  56.  {start}
  57. Var
  58.   a: Array Of Integer;
  59.   i,j,limit: Integer;
  60.   StartTime: TTime;
  61.   Elapsed: Int64;
  62.  
  63.  
  64. Begin
  65. limit:=100005;
  66.   setlength(a,3);
  67.   setup(a);
  68.  
  69.   StartTime := Time;
  70.   For j:=1 To limit Do
  71.     Begin
  72.       If (high(a) > 20) Then
  73.         Begin
  74.           setlength(a,3);
  75.           setup(a);
  76.         End;
  77.  
  78.       quicksort(a,0,high(a));
  79.       addone(a);
  80.      // write(high(a),' ');
  81.     End;
  82.   Elapsed := MillisecondsBetween(Time, StartTime);
  83.   Writeln('Time taken :',elapsed,' milliseconds for ',limit,' sorts');
  84.   writeln('Residual sorted array :');
  85.   For i:=0 To high(a) -1 Do   // don't want the addone
  86.     Writeln(a[i]);
  87.     writeln('Press enter to end . . .');
  88.   Readln;
  89. End.


Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Example Code Question
« Reply #18 on: April 24, 2019, 02:15:17 pm »
Maybe he forgot I just gave mathematically sound sorts for everyone yesterday? Better pay attention?
Specialize a type, not a var.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Example Code Question
« Reply #19 on: April 24, 2019, 04:17:53 pm »
Sorry I didn't see the your post for a couple of days.

The haversine Formula:

 It's for a project for X-Plane, a Flight Simulator. X-Plane has about 38,000 Airports. The current data record for an airport requires the City, Country and FAA Regional code placed in the data record. However, the older airports are grandfathered in and a lot don't contain the  City, Country and FAA Regional code info.

I have extracted the following from the airport records: Apt.DAt file 7.9 Million records

 1              2          3      4    5         6                  7                 <---- Field
[00CA][7826300][Nil][NIL][Nil][35.349333][-116.893333]       <----Data Record

Field: 1  Airport,  Field: 2  Hash into the 7.9 million file,  Field: 3  City 
Field: 4  Country,  Field: 5  FAA Regional Code,  Field: 6  Airport Latitude,  Field: 7  Airport Longitude

Of the 38,000 records about 17,000 are incomplete and 16,000 records are complete.
 
[KPHX][7566353][Phoenix][United States][K7][27.23342633][-80.97485154]

I have a demo partially working with the Haversine Formula:

Give it two locations and it will give you the distance between the two.

If I take the 16,000 complete records and do a haversine between the good records and one of the incomplete records I can at a min. determine the Country, FAA Regional Code and possibly the city.

Short answer: Validate some data records for a Flight Simulator.
I'm retired also.


 

« Last Edit: April 24, 2019, 04:20:25 pm by JLWest »
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Example Code Question
« Reply #20 on: April 24, 2019, 04:30:48 pm »
Maybe he forgot I just gave mathematically sound sorts for everyone yesterday? Better pay attention?

Yea, I saw it and copied it out to take a look at.

Thank you
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: Example Code Question
« Reply #21 on: April 24, 2019, 06:15:14 pm »

Thanks JLWEST.
I was a seaman, we used haversines for spherical trig. Likewise, basically, get a distance between a and b on a sphere.
Always positive 0 to 180 (suited logarithms), ambiguous between 0 and 360, but it was not a tall order to know which hemisphere you were in.

Thaddy
Microsoft pascal (quickpascal) has a sortdemo.pas.
Insertion, Bubble ,Heap, exchange, Shell and Quick.
It outputs pictorially with sound effects, and a menu is given for a choice.
Quickpascal is old now, perhaps an example of "the old ones are the best ones", for a demo anyway.

Come on Brexit, sort yourself out!











 

TinyPortal © 2005-2018