Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Delphi simple array question

Posted on 2011-05-01
Medium Priority
242 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

ID: 35500743
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

ID: 35500759
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

ID: 35500803
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

ID: 35500821
the listbox isnt populating, why would that be?
0

LVL 24

Expert Comment

ID: 35500825
0

LVL 24

Expert Comment

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

Author Comment

ID: 35500883
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

ID: 35500909
``````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

ID: 35500911
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

ID: 35500976
how can i output to tedit as opposed to listbox?
0

LVL 24

Expert Comment

ID: 35500993
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

ID: 35501015
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

ID: 35501049
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

ID: 35511218
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

jimyX earned 2000 total points
ID: 35511470
``````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 101

Expert Comment

ID: 35892783
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

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â€¦
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â€¦
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. â€¦
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give iâ€¦
###### Suggested Courses
Course of the Month12 days, left to enroll