Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

variable size arrays

Posted on 1998-06-03
6
Medium Priority
?
285 Views
Last Modified: 2010-04-06
In my program I am using an array[0..9,0..9] of Extended. But I would like to have the user to determine the size of the array, instead of pre-defining it.
It doesn't have to be an array. It can be a pointer or something that can be accessed as a bi-dimensional array, but with the size determined at run-time.
0
Comment
Question by:acerola
6 Comments
 
LVL 1

Author Comment

by:acerola
ID: 1350133
I tried that solution. It locked my machine. Than I took that idea, but used GetMem instead of that weird memory allocation procedure. It worked, but the program itself didn't. When I used an array larger than 4x4 the calculation result was always zero. Maybe it's because my program calls a function recursively.

Does anyone have any other suggestion?

Raised the points to 50.
0
 

Expert Comment

by:michael2010898
ID: 1350134
use variant type
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:acerola
ID: 1350135
This answer is way too generic. Be more specific (e.g. give me an source code). Just changing the variable type from extended to variant will not solve the problem.
0
 

Accepted Solution

by:
descikartus earned 200 total points
ID: 1350136
Try the following code:

program Project1;

uses
  SysUtils, Dialogs;

{$R *.RES}

type
  PDouble = ^Double;

// This will get a value from the array.
// All you have to do is pass the array, the width of
// the array, and the location within it that you want.
// This function assumes a two dimentional array, modify
// at will.
function GetDouble(const AD : array of Double; W, X, Y : Integer) : Double;
begin
  Result := AD[Y * W + X];
end;

// This works similar to the function above, except
// you are also passing it a value.
procedure SetDouble(PD : PDouble; W, X, Y : Integer; Value : Double);
begin
  PDouble(
      Integer(PD) +
      (Y * W * SizeOf(Double) + X * SizeOf(Double))
    )^ := Value;
end;

// Just some variable to show you how it works
var
  PADouble : PDouble;
  W, H : Integer;
  DoubleVar : Double;

begin

  // Set up our dynamic array
  W := 9;
  H := 9;
  GetMem(PADouble, H * W * SizeOf(Double));

  // Assign a value within it
  SetDouble(PADouble, W, 5, 5, 3.14159265);

  // Get the value back
  DoubleVar := GetDouble(PADouble^, W, 5, 5);

  // Display our value
  ShowMessage(FloatToStr(DoubleVar));

end.

Hopefully this helps.

0
 
LVL 1

Author Comment

by:acerola
ID: 1350137
Works just perfectly.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question