PMS Information Systems
Welcome to PMS Information Systems - IBM i (AS/400) Forum !!!

Get Answers for all your queries on IBM i (AS/400).

Join the forum, it's quick and easy

PMS Information Systems
Welcome to PMS Information Systems - IBM i (AS/400) Forum !!!

Get Answers for all your queries on IBM i (AS/400).
PMS Information Systems
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Finding Maximum and Minimum Values in RPG Arrays

Go down

Finding Maximum and Minimum Values in RPG Arrays Empty Finding Maximum and Minimum Values in RPG Arrays

Post  maran Sat Aug 11, 2012 12:35 am

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

maran
maran
Admin

Posts : 442
Join date : 2009-07-24

https://pmsinformationsystem.forumotion.net

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum