# Re: Delphi Array and MS/Access DB to store and retrieve data

[ DelphiLand FAQ ]

Posted by webmaster Guido on March 16, 2004 at 18:30:01:

In Reply to: Using a Delphi Array and MS/Access DB to store and retrieve data posted by Lionel Joyner P12219 on March 14, 2004 at 19:16:18:

: I have search the WWW trying to find an example that would fit what I am trying to do but I may be headed in the wrong direction and need an example to work with to set my direction.
: 1) I have a Number(Dist) and a Trig Function(Cos(X)and want to compute the (Y)Coordinate and then compute the (X) Coordinate by multiplying the Number(Dist) by the (Sin(X) The results for the first leg would be a ((+/-Y),(+/-X)) Coordinate.
: 2) Repeat the above and compute the coordinates for a second leg((+/-Y),(+/-X))
: 3)Each set of coordinates need to be stored and available for recall using M/SAccess

: An example (array with two legs in a repeat loop plus leg results storage in a DB) would be most helpful.
--------------------

If I understand you well, you want to calculate and store the coordinates of the end-points of two "legs", given:
- the distance between the points and the "origin" (0,0);
- and the angles that the legs make with the X-axis.

As you noted, calculating goes like this, to be repeated for each leg:
X := Dist * Sin(Angle);
Y := Dist * Cos(Angle);

If you store the given parameters and the resulting coordinates in an static array of records, that's the easiest.
Here's the record declaration:

```type
TLegPair = record
Dist1, Dist2, Angle1, Angle2, X1, Y1, X2, Y2: real;
end;```

We must declare how many "LegPairs":

```const
NrOfLegPairs = 100;```

Here's the array definition, using a static array:

```var
LegPairs: array [1..NrOfLegPairs] of TLegPair;```

The main loop, to input the data and store them:

```procedure DoTheLoop;
var
i: integer; // loop counter
begin
for i := 1 to NrOfLegPairs do begin
with LegPairs[i] do begin
// Get 2 distances and 2 angles from somewhere
Dist1  := ...;
Angle1 := ...;
Dist2  := ...;
Angle2 := ...;
// Calculate coordinates
X1 := Dist1 * Sin(Angle1);
Y1 := Dist1 * Cos(Angle1);
X2 := Dist2 * Sin(Angle2);
Y2 := Dist2 * Cos(Angle2);
end;
end;
end;```

Let's say that instead you want to save to a table T1 with eight fields of the type "real" (floating point number), named "Dist1", "Dist2", and so on. The main loop becomes:

```procedure DoTheLoop;
var
i: integer; // loop counter
begin
for i := 1 to NrOfLegPairs do begin
T1.Append; // create empty record
T1Dist1.Value  := ...;
T1Angle1.Value := ...;
T1Dist2.Value  := ...;
T1Angle2.Value := ...;
// Calculate coordinates
T1X1.Value := T1Dist1.Value * Sin(T1Angle1.Value);
T1Y1.Value := T1Dist1.Value * Cos(T1Angle1.Value);
T1X2.Value := T1Dist2.Value * Sin(T1Angle2.Value);
T1Y2.Value := T1Dist2.Value * Cos(T1Angle2.Value);
T1.Post;  // save the record to disk
end;
end;```

Questions:

Before constructing the loop, we must know:

- Is there a fixed number of "LegPairs"? If not, we can't use a static array to store the results, but we must use a structure with a dynamic size, such as: a dynamic array, a database on disk, a textfile, or a file-of-records. Of course, then also the structure of the loop changes, like this:

```while not ItsDone do begin
// Calculate records and store: ...
end;```

"ItsDone" can be a function that returns True or False. E.g. return False if no more data available, or if the user clicked a "Stop" button, whatever...

- How are the parameters Dist1, Dist2, Angle1, Angle2 obtained?
Are they entered in edit-boxes by the user, who clicks a button after each set of data?
Or are they obtained from a table on disk, or from a textfile on disk, or from one or more arrays in memory that were filled by some other part of the program?
Or are they calculated on the fly, whereby the next set of data is the previous set plus a given increment? Such as: Angle1 varies from 0 to a full circle in 10 increments, the same for Angle2, what would give us 100 sets of coordinates if Dist1 and Dist2 are fixed (10000 sets if D1 and D2 also vary in ten steps each)?

So if you give some more details, we could try to complete the code.

And forgive my curiosity ;-) why do you want to store these results in a database, for later recall? Why not you calculate them at the moment you need them, instead of reading the stored results from a slow database on disk?

Re: Using a Delphi Array and MS/Access DB to store and retrieve data lionel 3/17/2004
Re: Using a Delphi Array and MS/Access DB to store and retrieve data Lionel 3/17/2004
Re: Delphi Array and MS/Access DB Lionel 3/17/2004
Re: calculate data, store in database webmaster Guido 3/22/2004
Re: calculate data, store in database Lionel 3/23/2004
Re: resend example webmaster Guido 3/23/2004
Re: Problem Lionel 3/24/2004
Re: Problem webmaster Guido 3/24/2004
Re: Problems solved! Lionel Joyner 3/24/2004

[ DelphiLand: free Delphi source code, tips, tutorials ]