Solved

# Delphi simple array question

Posted on 2011-05-01
238 Views
I want to create an array with 30 random numbers between 0 and 100 and print to screen. How do I go about it? (new to programming)

0
Question by:lagg2007

LVL 24

Expert Comment

You can do it this way:
``````procedure TForm1.Button1Click(Sender: TObject);
var
A:array[1..30] of integer;
i:integer;
begin
Randomize;
for i:= 1 to 30 do
A[i] := Random(100);

for i:= 1 to 30 do
Showmessage('Element '+IntToStr(i)+ ' = '+IntToStr(A[i]));
end;
``````
0

LVL 24

Expert Comment

Showmessage may be a bit annoying specially with long array you may try using Listbox instead:
``````procedure TForm1.Button1Click(Sender: TObject);
var
A:array[1..30] of integer;
i:integer;
begin
Randomize;
for i:= 1 to 30 do
A[i] := Random(100);

for i:= 1 to 30 do
end;
``````
0

LVL 24

Expert Comment

For array lessons you may check these articles:
Article 1.
Article 2.

For the random numbers we use the Random function. Randomize is called first to initialize the random number generator.
Also as per Delphi help Random(x) will return any number in the range [0 and x-1] so in your case since you need a random number in the range 0 and 100, you can use Random(101):
So update line 8 to be:
``````    A[i] := Random(101);   // or 1 + Random(100)
``````
0

Author Comment

the listbox isnt populating, why would that be?
0

LVL 24

Expert Comment

0

LVL 24

Expert Comment

Here is a simple example:
Listbox---Array.zip
0

Author Comment

Code attached
``````unit fMinMax;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TfrmMinMax = class(TForm)
edArrayfill: TEdit;
btnFill: TButton;
lblMin: TLabel;
lblMax: TLabel;
lsbResult: TListBox;
private
{ Private declarations }
arDigits: array[1..30] of integer;
procedure FillArray;
//function GetMin: integer;
//function GetMax: integer;

public
{ Public declarations }
end;

var
frmMinMax: TfrmMinMax;

implementation

{\$R *.dfm}

procedure TfrmMinMax.FillArray;
var i:integer;

begin
randomize;
for i:=1 to 30 do
arDigits[i] := Random (101);

for i:= 1 to 30 do
end;
end.
``````
0

LVL 32

Expert Comment

``````unit fMinMax;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TfrmMinMax = class(TForm)
edArrayfill: TEdit;
btnFill: TButton;
lblMin: TLabel;
lblMax: TLabel;
lsbResult: TListBox;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
arDigits: array[1..30] of integer;
procedure FillArray;
//function GetMin: integer;
//function GetMax: integer;

public
{ Public declarations }
end;

var
frmMinMax: TfrmMinMax;

implementation

{\$R *.dfm}

procedure TfrmMinMax.FillArray;
var i:integer;

begin
randomize;
for i:=1 to 30 do
arDigits[i] := Random (101);

for i:= 1 to 30 do
end;

procedure TfrmMinMax.FormCreate(Sender: TObject);
begin
FillArray;
end;

end.
``````
0

LVL 24

Expert Comment

Although it is not necessary to write it that way, but your code is working except you need to call the procedure somewhere to populate the listbox. Add a button with calling the procedure in there:
``````procedure TfrmMinMax.Button1Click(Sender: TObject);
begin
FillArray;
end;
``````
0

Author Comment

how can i output to tedit as opposed to listbox?
0

LVL 24

Expert Comment

The edit box will show line of data, is that what you want?
Element 1= 61, Element 2= 53, Element 3= 52

Can you explain what you want to do exactly (I mean what is your idea or your requirement)?
0

Author Comment

yes that is what i would like to do, im not sure what property used to display, is it editbox.text()?

0

LVL 24

Expert Comment

OK you use the text property:
Edit1.Text := IntToStr(A[x]);

The above line will load only one element (element number x), but if you want to show all the array's elements in that Editbox then:
``````for i:= 1 to 30 do
Edit1.Text := Edit1.Text + IntToStr(A[i]);
``````

``````for i:= 1 to 30 do
if i < 30 then
Edit1.Text := Edit1.Text + IntToStr(A[i]) + ' '
else
Edit1.Text := Edit1.Text + IntToStr(A[i]);
``````
0

Author Comment

Thank you, I need to cycle through each item in the array and then find the largest and the smallest in two separate functions, how can I set this up
0

LVL 24

Accepted Solution

``````function AMax(Arr:array of integer):integer;
var
i:integer;
begin
result := Arr[low(Arr)];
for i := low(Arr) to high(Arr) do
begin
if Arr[i] > result then result := Arr[i];
end;
end;

function AMin(Arr:array of integer):integer;
var
i:integer;
begin
result := Arr[low(Arr)];
for i := low(Arr) to high(Arr) do
begin
if Arr[i] < result then result := Arr[i];
end;
end;
``````

Then you can use them as follows:
Edit1.Text := IntToStr(AMax(arDigits));    //as per your last code snippet "arDigits" is your Array
Edit2.Text := IntToStr(AMin(arDigits));
0

LVL 100

Expert Comment

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

## Featured Post

### Suggested Solutions

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…