Recent

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

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Getting the index of a min or max value of an array
« on: March 19, 2018, 11:15:54 pm »
I know that maxvalue provides the max value of an array and minvalue provides the min value of an array.
But, is there any function that gives me the index of the (min or max) value of an array?
« Last Edit: March 19, 2018, 11:19:46 pm by justnewbie »

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Getting the index of a min or max value of an array
« Reply #1 on: March 19, 2018, 11:26:18 pm »
YEs
 Low and High../

 Low(MyARray) returns the lowest index value

 High(MyArray) returns the highest index value

 I mean it reports the actually indexing bounds of the array...

 I think that is what you are after ?
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Getting the index of a min or max value of an array
« Reply #2 on: March 19, 2018, 11:39:20 pm »
Maybe you want

 IndexChar
 IndexWOrd
 IndexQWord

etc

 
The only true wisdom is knowing you know nothing

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: Getting the index of a min or max value of an array
« Reply #3 on: March 20, 2018, 12:00:22 am »
YEs
 Low and High../

 Low(MyARray) returns the lowest index value

 High(MyArray) returns the highest index value

 I mean it reports the actually indexing bounds of the array...

 I think that is what you are after ?

So, if my array contains integer numbers from 0 to 10 (in random order), and the 0 value's index is eg. 4, then low will give me 4?

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Getting the index of a min or max value of an array
« Reply #4 on: March 20, 2018, 12:06:21 am »
is there any function that gives me the index of the (min or max) value of an array?
No.

So, if my array contains integer numbers from 0 to 10 (in random order), and the 0 value's index is eg. 4, then low will give me 4?
No.

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Getting the index of a min or max value of an array
« Reply #5 on: March 20, 2018, 12:11:38 am »
No , please read my second post..

 You can cycle through the array looking for the value using a loop like this..
Code: Pascal  [Select][+][-]
  1.  
  2.  I := Low(MyArray);
  3.  While (I <= High(MyArry) ) and (MyArray[I] <> TheValueIamLookingFor) do In(I);
  4.  
"I" now points to the location, if not found for some reason "I" with be > High(MyArray);

------ Option two ----
Code: Pascal  [Select][+][-]
  1. Index := IndexDWord(MyArray, length(MyArray), DWord(MyIntegerIamLookingFor));
  2.  
  3. Integers under FprObj mode is 32 bit so the DWORD casting fits it..
  4.  
  5.  

The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Getting the index of a min or max value of an array
« Reply #6 on: March 20, 2018, 12:15:39 am »
I wanted to comment that the use of the second example will most likely give you a failing grade on this assignment ;)


Also the use of maxValue and Minvalue will also fail you ....

Your teacher wants you to do this in a loop using two variables.

One that remembers the last lowest number for example and one to travel down the list.
when you find one that is lower than the currently marked on you update that index.

 its pretty simple actually but I can tell you that using any sub functions to cover the  long hand code
is going to get you a failing Grade!

 Have fun..
« Last Edit: March 20, 2018, 01:00:06 am by jamie »
The only true wisdom is knowing you know nothing

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: Getting the index of a min or max value of an array
« Reply #7 on: March 20, 2018, 09:39:23 am »
Yes, I know that I can get it by using a loop, but was curious if in Pascal there is something similar to ArrayMaximum https://docs.mql4.com/array/arraymaximum and ArrayMinimum in MQL.

Thaddy

  • Hero Member
  • *****
  • Posts: 14162
  • Probably until I exterminate Putin.
Re: Getting the index of a min or max value of an array
« Reply #8 on: March 20, 2018, 12:23:05 pm »
justnewbie the answer is already given.... High(array) and Low(Array);
e.g:
Code: Pascal  [Select][+][-]
  1. program testarraybounds;
  2. {$mode objfpc}
  3. var
  4.    a:array[3..13] of integer;
  5.    b:array[100..999] of integer;
  6. begin
  7.     writeln(Low(a):5,high(a):5);
  8.     writeln(Low(b):5, High(b):5);
  9. end.
Specialize a type, not a var.

furious programming

  • Hero Member
  • *****
  • Posts: 836
Re: Getting the index of a min or max value of an array
« Reply #9 on: March 20, 2018, 01:02:49 pm »
@Thaddy: Low and High returns the lowest and the highest index of array, but OP needs the index of the lowest and the highest value from array. Two different things.

There is no functions that works the same like given ArrayMinimum and ArrayMaximum, so we need to write them:

Code: Pascal  [Select][+][-]
  1. function ArrayMinimum(const AValues: array of Integer): Integer;
  2. var
  3.   LValIdx: Integer;
  4.   LMinIdx: Integer = 0;
  5. begin
  6.   if Length(AValues) = 0 then Exit(-1);
  7.  
  8.   for LValIdx := 1 to High(AValues) do
  9.     if AValues[LValIdx] < AValues[LMinIdx] then
  10.       LMinIdx := LValIdx;
  11.  
  12.   Result := LMinIdx;
  13. end;
  14.  
  15. function ArrayMaximum(const AValues: array of Integer): Integer;
  16. var
  17.   LValIdx: Integer;
  18.   LMaxIdx: Integer = 0;
  19. begin
  20.   if Length(AValues) = 0 then Exit(-1);
  21.  
  22.   for LValIdx := 1 to High(AValues) do
  23.     if AValues[LValIdx] > AValues[LMaxIdx] then
  24.       LMaxIdx := LValIdx;
  25.  
  26.   Result := LMaxIdx;
  27. end;
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.

Josh

  • Hero Member
  • *****
  • Posts: 1270
Re: Getting the index of a min or max value of an array
« Reply #10 on: March 20, 2018, 01:36:59 pm »
Hi
Just a quick addition; the function has more parameters, starting index and how many; as well as constant whole_array
Code: Pascal  [Select][+][-]
  1. Const Whole_Array=0;
  2.  
  3. function ArrayMinimum(const AValues: array of Integer;
  4.                       acount,astart: LongInt): Integer;
  5. var
  6.   LValIdx: Integer;
  7.   LMinIdx: Integer;
  8.   LStart:Integer;
  9.   LEnd:Integer;
  10. begin
  11.   if Length(AValues) = 0 then Exit(-1);
  12.   If acount=Whole_Array then
  13.   begin
  14.     Lstart:=Low(AValues);
  15.     LEnd:=High(AValues);
  16.   end
  17.   else
  18.   begin
  19.     LStart:=aStart;
  20.     LEnd:=LStart+Acount;
  21.     If Lstart<Low(Avalues) then Lstart:=Low(Avalues);  // not sure if to exit out with a -1 if out of range
  22.     If LEnd>High(Avalues) then LEnd:=High(Avalues); // not sure if to exit out with a -1 if out of range
  23.   end;
  24.   LMinIdx:=LStart; // set LminIdx to Start Position
  25.   for LValIdx := LStart to LEnd do
  26.     if AValues[LValIdx] < AValues[LMinIdx] then
  27.       LMinIdx := LValIdx;
  28.  
  29.   Result := LMinIdx;
  30. end;        
« Last Edit: March 20, 2018, 01:56:33 pm by josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: Getting the index of a min or max value of an array
« Reply #11 on: March 20, 2018, 02:57:30 pm »
@furious programming: yes, you understand well my question, your function is great

@josh: yes, the additions (start index and number of elements) are important for the full functionality

Thank you guys!

(Maybe, is here anyone who is familiar with MQL as well? I need to make some data changing between FP and MQL.)
« Last Edit: March 20, 2018, 03:01:14 pm by justnewbie »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Getting the index of a min or max value of an array
« Reply #12 on: March 20, 2018, 04:28:27 pm »
Let's assume you intended this thread to be only about arrays of integer variables.

Note that "minimum" and "maximum" may be undefined (or at least ambiguous) depending on the size of the array and the distribution of data it holds. Hence josh's desire to add parameters to the function to gain at least some minimal information about the data you are going to give the function in order to produce the statistic.
What is the maximum value of an empty array?
Given the array [5, 5, 5], what is the index of its maximum value?

For sorted arrays, the built-in Low() and High() functions do give you the indices of the minimum and maximum values. Well, strictly, the first index of a minimum value and the last index of a maximum value, when the sorted array contains duplicates (and is sorted in ascending order).
This is obviously faster than iterating the entire array looking for a maximum/minimum value (but only because array iterations have already taken place to produce the sorted array).

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: Getting the index of a min or max value of an array
« Reply #13 on: March 20, 2018, 04:34:33 pm »
In fact, this is not a sorted Integer array, this is an unsorted Double array.  :)

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Getting the index of a min or max value of an array
« Reply #14 on: March 20, 2018, 11:28:13 pm »
I gave you "IndexDword" just cast our integers to a DWORD when searching.

Actually, the compiler may allow this without casting..

 That will report the INDEX of where the first index is found of that value.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018