Finding Maximum and Minimum Values in RPG Arrays
Page 1 of 1
Finding Maximum and Minimum Values in RPG Arrays
Finding Maximum and Minimum Values in RPG Arrays
In cases where all elements of an RPG array are filled with some values, you find maximum and minimum values of the array simply after their sorting. (So, the first element would contain the min value and the last, max.) But usually the number of considered values is variable while the number of array elements in RPG cannot be variable. That limitation makes it challenging to find maximum and minimum values of an array. Having positive and negative values in the array elements adds further to the challenge.
This piece presents formulas for calculating max and min values of an array—A[1], A[2], ... ,A[n], ... , A[N],—where n is the number of variables from which to select max and min, and N is the total number of array elements.
The formulas are based on these assumptions:
1. The arrays are first sorted by SORTA.
2. The number of array elements is greater than double the number of variables: N > 2n.
3. Initial values of the array elements are zeros.
4. Elements could contain both positive and negative values.
Here are the formulas:
Max = A[N] + A[n]
Min = A[N-n+1] + A[1]
You can use these formulas for arrays containing both positive and negative numbers. The examples below show finding max and min from four given values (n=4) using an array with 10 elements (N=10) when all values are positive (example 1), all values are negative (example 2), and some values are positive and some negative (example 3).
Example 1. All values are positive: 4, 1, 9, 3.
A[1]=4, A[2]=1, A[3]=9, A[4]=3, A[5]=0, A[6]=0, A[7]=0, A[8]=0, A[9]=0, A[10]=0
Below is the array after sorting:
A[1]=0, A[2]=0, A[3]=0, A[4]=0, A[5]=0, A[6]=0, A[7]=1, A[8]=3, A[9]=4, A[10]=9 Max = A[N] + A[n] = A[10] + A[4] = 9 + 0 = 9
Min = A[N-n+1] + A[1] = A[7] + A[1] = 1 + 0 = 1
Example 2. All values are negative: -4, -1, -9, -3.
A[1]=-4, A[2]=-1, A[3]=-9, A[4]=-3, A[5]=0, A[6]=0, A[7]=0, A[8]=0, A[9]=0, A[10]=0
Below is the array after sorting:
A[1]=-9, A[2]=-4, A[3]=-3, A[4]=-1, A[5]=0, A[6]=0, A[7]=0, A[8]=0, A[9]=0, A[10]=0 Max = A[N] + A[n] = A[10] + A[4] = 0 + (-1) = -1 Min = A[N-n+1] + A[1] = A[7] + A[1] = 0 + (-9) = -9
Example 3. Some values are positive and some are negative: -4, 1, -9, 3.
A[1]=-4, A[2]=1, A[3]=-9, A[4]=3, A[5]=0, A[6]=0, A[7]=0, A[8]=0, A[9]=0, A[10]=0
Below is the array after sorting:
A[1]=-9, A[2]=-4, A[3]=0, A[4]=0, A[5]=0, A[6]=0, A[7]=0, A[8]=0, A[9]=1, A[10]=3 Max = A[N] + A[n] = A[10] + A[4] = 3 + 0 = 3 Min = A[N-n+1] + A[1] = A[7] + A[1] = 0 + (-9) = -9
In cases where all elements of an RPG array are filled with some values, you find maximum and minimum values of the array simply after their sorting. (So, the first element would contain the min value and the last, max.) But usually the number of considered values is variable while the number of array elements in RPG cannot be variable. That limitation makes it challenging to find maximum and minimum values of an array. Having positive and negative values in the array elements adds further to the challenge.
This piece presents formulas for calculating max and min values of an array—A[1], A[2], ... ,A[n], ... , A[N],—where n is the number of variables from which to select max and min, and N is the total number of array elements.
The formulas are based on these assumptions:
1. The arrays are first sorted by SORTA.
2. The number of array elements is greater than double the number of variables: N > 2n.
3. Initial values of the array elements are zeros.
4. Elements could contain both positive and negative values.
Here are the formulas:
Max = A[N] + A[n]
Min = A[N-n+1] + A[1]
You can use these formulas for arrays containing both positive and negative numbers. The examples below show finding max and min from four given values (n=4) using an array with 10 elements (N=10) when all values are positive (example 1), all values are negative (example 2), and some values are positive and some negative (example 3).
Example 1. All values are positive: 4, 1, 9, 3.
A[1]=4, A[2]=1, A[3]=9, A[4]=3, A[5]=0, A[6]=0, A[7]=0, A[8]=0, A[9]=0, A[10]=0
Below is the array after sorting:
A[1]=0, A[2]=0, A[3]=0, A[4]=0, A[5]=0, A[6]=0, A[7]=1, A[8]=3, A[9]=4, A[10]=9 Max = A[N] + A[n] = A[10] + A[4] = 9 + 0 = 9
Min = A[N-n+1] + A[1] = A[7] + A[1] = 1 + 0 = 1
Example 2. All values are negative: -4, -1, -9, -3.
A[1]=-4, A[2]=-1, A[3]=-9, A[4]=-3, A[5]=0, A[6]=0, A[7]=0, A[8]=0, A[9]=0, A[10]=0
Below is the array after sorting:
A[1]=-9, A[2]=-4, A[3]=-3, A[4]=-1, A[5]=0, A[6]=0, A[7]=0, A[8]=0, A[9]=0, A[10]=0 Max = A[N] + A[n] = A[10] + A[4] = 0 + (-1) = -1 Min = A[N-n+1] + A[1] = A[7] + A[1] = 0 + (-9) = -9
Example 3. Some values are positive and some are negative: -4, 1, -9, 3.
A[1]=-4, A[2]=1, A[3]=-9, A[4]=3, A[5]=0, A[6]=0, A[7]=0, A[8]=0, A[9]=0, A[10]=0
Below is the array after sorting:
A[1]=-9, A[2]=-4, A[3]=0, A[4]=0, A[5]=0, A[6]=0, A[7]=0, A[8]=0, A[9]=1, A[10]=3 Max = A[N] + A[n] = A[10] + A[4] = 3 + 0 = 3 Min = A[N-n+1] + A[1] = A[7] + A[1] = 0 + (-9) = -9
Similar topics
» Find a String in a DBF records.
» What is the maximum number of PRTOBJ functions that can be added to one PRTFIL function?
» SQL TIP: FINDING TOTAL NO OF FEMALE AND MALE EMPLOYEES USING CASE
» System Values
» want to change system values
» What is the maximum number of PRTOBJ functions that can be added to one PRTFIL function?
» SQL TIP: FINDING TOTAL NO OF FEMALE AND MALE EMPLOYEES USING CASE
» System Values
» want to change system values
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|