Solved

variable size arrays

Posted on 1998-06-03
6
274 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 5

Expert Comment

by:ronit051397
ID: 1350132
0
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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 50 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now