Solved

variable size arrays

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses
Course of the Month9 days, 21 hours left to enroll

623 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