Entry
Delphi: Array: 1D: Search: Simple: How search in a 1-dimensional array? [parameter pass procedure]
Nov 14th, 2003 08:22
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 10 November 2003 - 10:23 pm -------------------
Delphi: Array: 1D: Search: Simple: How search in a 1-dimensional
array? [parameter pass procedure]
---
Steps: Overview:
1. -Create a new application in Delphi
2. -Put a button on the form
3. -Double click on the button
4. -As data e.g. the following is given.
The 1-dimensional array with its data
[0] = 0
[1] = 1
[2] = 2
[3] = 3
[4] = 4
[5] = 5
[6] = 6
[7] = 7
[8] = 8
and the element you are looking for
is e.g. 4.
5. -To search use e.g. a linear search
through all the elements of the array
until the element is found
Thus in pseudocode:
for column = columnfirst to columnlast
if element is found in array( column ) then message found
end
6. -Thus this might become in Delphi:
var j : integer;
begin
for i := Low( myarray ) to High( myarray ) do begin
if ( myarray[ i ] ) = integersearchI then begin
ShowMessage( Format( '[ %d ] -> %d', [ i, myarray[ i ] ] ) );
end;
end;
end;
6. -So all together this might give the following source code:
--- cut here ---------------------------------------------------------
// --- MAIN --- //
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, StdCtrls;
type
Tmyarray = array[0..8] of integer;
type
TForm1 = class(TForm)
Button1: TButton;
// --- FUNCTION: HEADER: BEGIN --- //
procedure Button1Click(Sender: TObject);
procedure PROCArrayInitializeSearchElement( var myarray :
Tmyarray );
procedure PROCArraySearchElementMain;
procedure PROCArrayViewSearchElement( integersearchI : integer;
myarray : Tmyarray );
// --- FUNCTION: HEADER: END --- //
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
// Set range checking on
{$R+}
procedure TForm1.Button1Click(Sender: TObject);
begin
PROCArraySearchElementMain;
end;
// --- LIBRARY --- //
// library: array: view: search: element [kn, ni, fr, 14-11-2003
15:14:22]
procedure TForm1.PROCArrayInitializeSearchElement( var myarray :
Tmyarray );
begin
myarray[ 0 ] := 0;
myarray[ 1 ] := 1;
myarray[ 2 ] := 2;
myarray[ 3 ] := 3;
myarray[ 4 ] := 4;
myarray[ 5 ] := 5;
myarray[ 6 ] := 6;
myarray[ 7 ] := 7;
myarray[ 8 ] := 8;
end;
// library: array: view: search: element [kn, ni, fr, 14-11-2003
15:14:22]
procedure TForm1.PROCArraySearchElementMain;
// e.g. // Set range checking on
// e.g. {$R+}
// e.g.
// e.g. type
// e.g. Tmyarray = array[0..8] of integer;
// e.g.
// e.g. PROCArraySearchElementMain;
var myarray : Tmyarray;
begin
PROCArrayInitializeSearchElement( myarray );
PROCArrayViewSearchElement( 4, myarray );
end;
// library: array: view: search: element [kn, ni, fr, 14-11-2003
15:14:22]
procedure TForm1.PROCArrayViewSearchElement( integersearchI : integer;
myarray : Tmyarray );
// e.g. type
// e.g. Tmyarray = array[0..8] of integer;
// e.g.
// e.g. var myarray : Tmyarray;
// e.g. PROCArrayViewSearchElement( 4, myarray );
var i : integer;
var j : integer;
begin
for i := Low( myarray ) to High( myarray ) do begin
if ( myarray[ i ] ) = integersearchI then begin
ShowMessage( Format( '[ %d ] -> %d', [ i, myarray[ i ] ] ) );
end;
end;
end;
end.
--- cut here ---------------------------------------------------------
6. -If you run this program, you will see a message box showing:
+-----------------------+
| |
| Found 4 at [ 4 ] |
| |
| +------+ |
| | OK | |
| +------+ |
| |
+-----------------------+
---
---
Internet: see also:
---
Delphi: Array: 2D: Search: Simple: How search in a 2-dimensional
array? [parameter pass procedure]
http://www.faqts.com/knowledge_base/view.phtml/aid/26482/fid/175
----------------------------------------------------------------------