?
Solved

variable size arrays

Posted on 1998-06-03
6
Medium Priority
?
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

719 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