Recent

Author Topic: Getting the index of a min or max value of an array  (Read 20350 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: Getting the index of a min or max value of an array
« Reply #15 on: March 21, 2018, 07:53:53 am »
Note from math unit:
Code: Pascal  [Select][+][-]
  1. { Min/max determination }
  2. function MinIntValue(const Data: array of Integer): Integer;
  3. function MaxIntValue(const Data: array of Integer): Integer;

And a complete series of minvalue and maxvalue.
« Last Edit: March 21, 2018, 08:00:40 am by Thaddy »
Specialize a type, not a var.

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: Getting the index of a min or max value of an array
« Reply #16 on: March 21, 2018, 06:04:53 pm »
Assume, I need procedure instead of function, and I want to write the LMinIdx value into a pointer (and I want to use this value in an other procedure).

Code: Pascal  [Select][+][-]
  1. procedure ArrayMinimum(const AValues: array of Integer; ...MyPointer...);
  2. var
  3.   LValIdx: Integer;
  4.   LMinIdx: Integer = 0;
  5. begin
  6.   if Length(AValues) = 0 then Exit;
  7.  
  8.   for LValIdx := 1 to High(AValues) do
  9.     if AValues[LValIdx] < AValues[LMinIdx] then
  10.       LMinIdx := LValIdx;
  11.  
  12.      ... MyPointer gets the LMinIdx here ...
  13.  
  14. end;

How does this procedure look like right way?

« Last Edit: March 21, 2018, 06:11:26 pm by justnewbie »

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: Getting the index of a min or max value of an array
« Reply #17 on: March 21, 2018, 06:21:22 pm »
How does this procedure look like right way?
Open arrays are indexed from zero. There is no need for pointers, you can declare that the parameter is an output value:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$MODE OBJFPC}
  3. {$APPTYPE CONSOLE}
  4.  
  5. procedure ArrayMinimum(const AValues: array of Integer; out LastIndexOfMinValue: Integer);
  6. var
  7.   LValIdx: Integer;
  8.   LMinIdx: Integer;
  9. begin
  10.   LMinIdx := High(AValues); // -1 for empty arrays
  11.   for LValIdx := High(AValues) - 1 downto Low(AValues) do
  12.     if AValues[LValIdx] < AValues[LMinIdx] then
  13.       LMinIdx := LValIdx;
  14.   LastIndexOfMinValue := LMinIdx;
  15. end;
  16.  
  17. var
  18.   LastIndexOfMinValue: Integer;
  19. begin
  20.   ArrayMinimum([2, 2, 7, 8, 3, 2], LastIndexOfMinValue);
  21.   Writeln(LastIndexOfMinValue);
  22.   ArrayMinimum([], LastIndexOfMinValue);
  23.   Writeln(LastIndexOfMinValue);
  24.   Readln;
  25. end.
« Last Edit: March 21, 2018, 06:25:06 pm by ASerge »

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: Getting the index of a min or max value of an array
« Reply #18 on: March 21, 2018, 06:58:12 pm »
@ASerge: perfect, exactly what I need. Thank you!

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: Getting the index of a min or max value of an array
« Reply #19 on: March 21, 2018, 10:18:07 pm »
Just a quick question:
What is the difference between these:

function Something(Val: Integer): Boolean;

and

function Something(Var Val: Integer): Boolean;

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: Getting the index of a min or max value of an array
« Reply #20 on: March 21, 2018, 10:35:21 pm »

Why do you insist on giving somewhat proper advice, but provide less efficient code than the default as is provided in math? Just curious.... O:-) 8-) >:D
Specialize a type, not a var.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Getting the index of a min or max value of an array
« Reply #21 on: March 21, 2018, 10:41:04 pm »
What is the difference between these:

function Something(Val: Integer): Boolean;

and

function Something(Var Val: Integer): Boolean;

In the first case, the function Something receives copy of the parameter Val. Although you can change Val's value within Something, any changes you make to Val are lost as soon as you leave the function. This is called passing a parameter by value.

In the second case, the parameter is an actual reference to the original variable Val. Any changes you make to Val within Something affect Val permanently, because you are not dealing with a temporary copy of Val, but with Val itself. This is called passing a parameter by reference.

In other words, reading the value of Val is identical in both parameter conventions, but writing to Val, although allowed when passing by value, has no permanent effect on the value of Val in the first case, whereas writing to Val when passed by reference changes its value.

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: Getting the index of a min or max value of an array
« Reply #22 on: March 21, 2018, 10:44:06 pm »

Why do you insist on giving somewhat proper advice, but provide less efficient code than the default as is provided in math? Just curious.... O:-) 8-) >:D

I've already said: it is not an integer array, but double. (The example is just an example, no problem the integer in it.)
Maybe is there a variant for floatings as well?
« Last Edit: March 21, 2018, 10:48:32 pm by justnewbie »

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: Getting the index of a min or max value of an array
« Reply #23 on: March 21, 2018, 10:46:44 pm »
@howardpc: great introducing, now it is clear, thank you!

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Getting the index of a min or max value of an array
« Reply #24 on: March 21, 2018, 11:04:43 pm »
Passing Via CONST allows the compiler to make the best choice of passing the item...

 It could be Value or it could be by reference, but its not going to allow you to change the value of it.
The only true wisdom is knowing you know nothing

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: Getting the index of a min or max value of an array
« Reply #25 on: March 22, 2018, 06:57:26 am »
Why do you insist on giving somewhat proper advice, but provide less efficient code than the default as is provided in math? Just curious.... O:-) 8-) >:D
There is no such function in math.
Topic: Getting the index of a min or max value of an array. INDEX! And you all the time value, value.

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: Getting the index of a min or max value of an array
« Reply #26 on: March 22, 2018, 10:11:26 am »
There is no such function in math.
Topic: Getting the index of a min or max value of an array. INDEX! And you all the time value, value.
Exactly. My task was to make the equivalent to this: https://docs.mql4.com/array/arraymaximum

furious programming

  • Hero Member
  • *****
  • Posts: 836
Re: Getting the index of a min or max value of an array
« Reply #27 on: March 22, 2018, 01:53:07 pm »
Exactly. My task was to make the equivalent to this: https://docs.mql4.com/array/arraymaximum

Hmm… arraymaximum from link is a function, not a procedure, that means it must return something different than void. Exactly, it returns the index of largest value, as I show in my previous code. But I didn't implement other params, so here is the final equivalent:

Code: Pascal  [Select][+][-]
  1. uses
  2.   SysUtils;
  3.  
  4. const
  5.   WHOLE_ARRAY = Integer.MaxValue;
  6.  
  7.   function ArrayMaximum(const AArray: array of Integer; AIndex: Integer; ACount: Integer = WHOLE_ARRAY): Integer;
  8.   var
  9.     LMaxIdx: Integer;
  10.   begin
  11.     if (AIndex < 0) or (AIndex >= Length(AArray)) or (ACount < 1) then Exit(-1);
  12.  
  13.     LMaxIdx := AIndex;
  14.  
  15.     while (AIndex < Length(AArray)) and (ACount > 0) do
  16.     begin
  17.       if AArray[AIndex] > AArray[LMaxIdx] then
  18.         LMaxIdx := AIndex;
  19.  
  20.       AIndex += 1;
  21.       ACount -= 1;
  22.     end;
  23.  
  24.     Result := LMaxIdx;
  25.   end;

Prepare code to use other data types (and write ArrayMinimum, based on this function).
« Last Edit: March 22, 2018, 01:58:48 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: Getting the index of a min or max value of an array
« Reply #28 on: March 22, 2018, 03:07:03 pm »
I changed it from function to procedure, because I need to get both the index and the value[index], and the "out" is perfect for me to get these values and reuse them outside the procedure.
Anyway, you are showing a great solution!
« Last Edit: March 22, 2018, 03:08:41 pm by justnewbie »

 

TinyPortal © 2005-2018