SRL Archive - OSR Help

Highest value in an array


Chris

2012-01-17 11:04:28

Its Solved :D


Hi, I am searching for a function :bart:
Well, I have an "array of Integer" and the array contains values but I need a function which finds the highest integer inside the array.
Like this:
 
var
A:array of Integer;
begin
A[0] := 6;
A[1] := 2;
A[2] := 9;
A[3] := 16;
A[4] := 23;
A[5] := 3;
writeln('A[' + HighestValue(A) + '] contains the highest value.');
end;

which would return:
A[4] contains the highest value.

But HighestValue doesnt exist so is there a function which does this?

RISK

2012-01-17 11:08:08

High(A);

Chris

2012-01-17 11:08:50

From: RISK
High();


if I do High(A) it would return 5, right?

Flight

2012-01-17 11:10:24

Um… just skimming through the math functions in Simba, have you tried MaxA?
function MaxA(a: TIntegerArray): Integer;

I've never tried it nor heard of it, but it might be worth a shot, no?

RISK

2012-01-17 11:10:25

program new;

procedure Example;
var
I: Integer;
arr: TIntegerArray;
begin
arr := [0, 1, 2, 3, 4, 5];

for I := 0 to High(arr) do
begin
WriteLn('arr: ' + ToStr(I));
end;
end;

begin
Example;
end.

Try it out.

Chris

2012-01-17 11:14:16

From: Flight
Um… just skimming through the math functions in Simba, have you tried MaxA?
function MaxA(a: TIntegerArray): Integer;

I've never tried it nor heard of it, but it might be worth a shot, no?


@RISK,
arr: 0
arr: 1
arr: 2
arr: 3
arr: 4
arr: 5
And thats not what I need :P

@Fllight, I will try it

RISK

2012-01-17 11:15:08

Oh, oops. I'm sorry. I'm half asleep. :x
What Flight gave you is probably what you require.

Chris

2012-01-17 11:23:00

From: RISK
Oh, oops. I'm sorry. I'm half asleep. :x
What Flight gave you is probably what you require.


ok no problem :p

and Ive tried it like this:
program new;

procedure Example;
var
//A: Integer;
A: array of integer;
begin
A := [4, 1, 8, 17, 4, 5];
writeln(MaxA(A));
end;

begin
Example;
end.
and it gives me 17, Thats almost what I need
I will use a "for" loop to find which number it is in this case its "3" :p

Thanks! :D

footballjds

2012-01-17 11:29:38

From: Chris
ok no problem :p

and Ive tried it like this:
program new;

procedure Example;
var
//A: Integer;
A: array of integer;
begin
A := [4, 1, 8, 17, 4, 5];
writeln(MaxA(A));
end;

begin
Example;
end.
and it gives me 17, Thats almost what I need
I will use a "for" loop to find which number it is in this case its "3" :p

Thanks! :D


Make sure your logic is correct. If you have an array with 30 values and 25 of them are the same do you really want it to return the array that had the highest?

Chris

2012-01-17 11:34:58

From: footballjds
Make sure your logic is correct. If you have an array with 30 values and 25 of them are the same do you really want it to return the array that had the highest?


Ive thought about that, but that doesnt occur in my script :)

footballjds

2012-01-17 11:39:16

From: Chris
Ive thought about that, but that doesnt occur in my script :)


Then i wouldn't suggest using ONLY what what flight posted as you'll have to go through the loop again to figure out which one has the value of 17.

Chris

2012-01-17 11:45:02

From: footballjds
Then i wouldn't suggest using ONLY what what flight posted as you'll have to go through the loop again to figure out which one has the value of 17.


I am using the function Flight posted, but it searches through my array for the highest value in it while I need the number which the highest value contains.
Thats why I have now made a loop to find that after I used MaxA(A)

woot 100 posts :P

superuser

2012-01-17 13:05:31

function HighestIndex(a: TIntegerArray): integer;
var
i, h: integer;
begin
result := -1;

if (Length(a) < 1) then
exit;

h := a[0];
result := 0;

for i := 0 to High(a) do
if (a[i] > h) then begin
result := i;
h := a[i];
end;
end;

var
a: TIntegerArray;

begin
SetLength(a, 5);

a[0] := 5;
a[3] := 8;

WriteLn(IntToStr(HighestIndex(a)));
end.

Shuttleu

2012-01-17 13:10:06

From: superuser
function HighestIndex(a: TIntegerArray): integer;
var
i, h: integer;
begin
result := -1;

if (Length(a) < 1) then
exit;

h := a[0];
result := 0;

for i := 0 to High(a) do
if (a[i] > h) then begin
result := i;
h := a[i];
end;
end;

var
a: TIntegerArray;

begin
SetLength(a, 5);

a[0] := 5;
a[3] := 8;

WriteLn(IntToStr(HighestIndex(a)));
end.


function HighestIndex(a: TIntegerArray): integer;
var
i: integer;
begin
result := -1;

if (Length(a) < 1) then
exit;

for i := 0 to High(a) do
if (a[result] < a[i]) then
result:= i;
end;

superuser

2012-01-17 13:13:27

From: Shuttleu
function HighestIndex(a: TIntegerArray): integer;
var
i: integer;
begin
result := -1;

if (Length(a) < 1) then
exit;

for i := 0 to High(a) do
if (a[result] < a[i]) then
result:= i;
end;


function HighestIndex(a: TIntegerArray): integer;
var
i: integer;
begin
result := -1;

if (Length(a) < 1) then
exit;

result := 0;

for i := 0 to High(a) do
if (a[result] < a[i]) then
result:= i;
end;

Shuttleu

2012-01-17 13:21:12

From: superuser
function HighestIndex(a: TIntegerArray): integer;
var
i: integer;
begin
result := -1;

if (Length(a) < 1) then
exit;

result := 0;

for i := 0 to High(a) do
if (a[result] < a[i]) then
result:= i;
end;


why have the result:= 0?

if the array is empty then it will be -1
otherwise it will be the highest one
at the very least it will be a[0]

~shut

Method

2012-01-17 13:42:58

From: Shuttleu
why have the result:= 0?

if the array is empty then it will be -1
otherwise it will be the highest one
at the very least it will be a[0]

~shut


Unless array[-1] is valid in Pascal, your snippet shouldn't work properly.

Shuttleu

2012-01-17 13:46:10

From: Method
Unless array[-1] is valid in Pascal, your snippet shouldn't work properly.


oh yeah, i wasnt thinking properly :s

~shut