Please provide me the solution or at least a hint to this problem.
Lets Suppose I have an array having 4 elements ( A, B, C, D). I would like to have all the possible combination of the 4 elements with no repetition (i.e. in mathematical terms 4C1 + 4C2 + 4C3 + 4C4 = 15).
How can we do this in RPGLE? Please reply ASAP as my whole project delivery is stuck up because of this requirement. And please provide me a generic solution as the array can have maximum of 10 elements.
Thanks in Anticipation. Best Regards.
Moderator Note: This question was submitted to our moderator mailbox.

Asked:
September 16, 2008 2:58 PM
Last updated:
July 31, 2010 4:13 AM

Thanks. We'll let you know when a new response is added.

Sorry, you are right, I did miss that one 4c4. I still think the link is usefull. It clearly illustrates the pattern. It would be farly simple to program I think. if you had 4 elemts say a,b,c, and d. then you would start with the first one A and go B,C,D, then increment a counter and start with the second one B and add C and D then the last one woud be CD. that would be taking them two at a time, then do it three at a time then the last time is four at a time, then so on.. you would need some do loops or FOR loops,… seems fairly straight forward. Wish I had time to program it, sounds like an intresting exercise in using loops.

Sorry, you are right, I did miss that one 4c4. I still think the link is usefull. It clearly illustrates the pattern. It would be farly simple to program I think. if you had 4 elemts say a,b,c, and d. then you would start with the first one A and go B,C,D, then increment a counter and start with the second one B and add C and D then the last one woud be CD. that would be taking them two at a time, then do it three at a time then the last time is four at a time, then so on.. you would need some do loops or FOR loops,... seems fairly straight forward. Wish I had time to program it, sounds like an intresting exercise in using loops.

Please enter an answer.

Send me notifications when members answer or reply to this question.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States.
Privacy

Improve This Answer

Improve This Answer

Processing your response...

Discuss This Question: 10 Replies

There was an error processing your information. Please try again later.

Thanks. We'll let you know when a new response is added.

Please enter a reply.

Send me notifications when members answer or reply to this question.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States.
Privacy

I would make it more specific.
Suppose we are having an array with names:
Karan
Julie
Mark
Robert
Since there are 4 elements in the array for my further requirement I need to have all the non-repetitive combination of these Four names.
Going with mathematical formula for Combination we have the following output for 4 elements i.e.
NC1 + NC2 + NC3 + NC4........NCN or 2**n - 1
Which for our case comes out to be 15.
So there could be 15 combination of four names in the array.
Like if we consider all the 4 elements for Combination we have Karan+Julie+Mark+Robert as one Combination which is equivalent to 4C4.
And if we consider 3 elements for Combination we wil have 4 combinations:
Karan+Julie+Mark
Karan+Julie+Robert
Julie+ Mark+Robert
Karan + Mark + Robert
Which is equivalent to 4C3.
And if we consider 2 elements for Combination we wil have 6 combinations:
Karan+Julie
Julie+Robert
Mark+Robert
Julie + Mark
Karan + Mark
Karan + Robert
Which is equivalent to 4C2.
And if we consider 1 elements for Combination we wil have 4 combinations:
Karan
Julie
Mark
Robert
Which is equivalent to 4C1.
So Sum of all these combination comes out to be 15.
For this we need to have a generic logic for the combinations of N number of elements in the array.
Hope this will Help!!

if it is the combinations of names you need rather than just the number 15, then here is a link to a really good explanation of how to get the different combinations. it includes an algorithm and enough informations on the patterns that you should be able to produce some RPG code to do it. It doesn't look too difficult.

Thanx..
But I could not find anything Substantial from the link.
I think you missed reading one combination i.e.4C4 which is equal to 1.
Please any other ideas/help you can give..that would be great...
Thanks,
Karan

perhaps this old google answers link would be more usefull. it includes a more detaild algorithm
http://74.125.45.104/search?q=cache:tAbxiPYw8poJ:google.com/answers/threadview%3Fid%3D392914+unique+combinations+algorithm&hl=en&ct=clnk&cd=1&gl=us

D wrka S 10 dim(10) incoming array
D result S 100 dim(100) array to be loaded and tested for uniqueness
numEl is position of first blank element - 1 (10 if no blank elements)
I'm sure this could be similified with recursion but it's 10 little code snipets like this one.
Then the number of unique entries is the first blank in result array -1
I think you need to sort wrka before you begin.
*--------- process 3 elements
C if numEl > 2
C for x = 1 to numEl - 2
C for y = 2 to numEl - 1
C for z = 3 to numEl
C eval wrkPos = %lookup(*blanks: result)
C
C eval wrkStr = wrka(x) + wrka(y) + wrka(z)
C if %lookup(wrkStr : result) = *zero
C eval result(wrkPos) = wrkStr
C endif
C endfor
C endif
C endsr

this is pseudocode so translate to RPG
Let's have the contents be A,B,C,D instead of names just to make this easier to type.
call the array ar so 4 elements ar1 ='A' ar2='B' ar3='C' and ar4='D'
N=4 the number of elements and K=2 the number of two combinations so we will get the result AB AC AD BC BD and CD
here are the for loops.
for x1 = 1 to N-1 note: n-1 is 3 in this case
for x2 = (x1+1) to N note: this is from 2 to 4
ar(x1)+ar(x2) = 'A'+'B' = 'AB'
next
next
first time through x1=1 so you get A, and x2=1 so you get B 'AB' x2 then continues to increment so you then get 'AC' 'AD' 'AE' then x1 increments so you get 'B' then x2 gives you 'BC' then 'BD', then x1 increments and you get 'C' and x2 gives you 'CD'
then you would have to do another set of for loops for k=3 and k=4 but since these loops are so similar you could probably put them in a subrouting and pass in the N and K values therby making it more generic.
hope this helps:

For all combinations, this can simply be represented as all binary numbers of 4 digits or less. If the array has ten elements, then it would be all binary numbers of ten digits or less.
The value 0 can be excluded from any such list, which is shown as the "- 1" part of "2**n - 1".
The idea is that each binary position represents one of the elements in the array. If the first binary digit is a "1", then the first element of the array is in that combination.
Assume the array contains 'A', 'B', 'C' and 'D'. Then the binary number 0011 represents the combination 'C' and 'D'. The number 1010 is 'A' and 'C'. And 1111 is 'A', 'B', 'C' and 'D'.
In short, just generate all binary numbers up to the limit of digits needed. This corresponds to powers of 2. Then grab the associated array elements.
Quick and easy.
Tom

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States.
Privacy

Reply

Processing your reply...

Ask a Question

Question Title:(150 char. limit.) Question:(Please be specific.)

Free Guide: Managing storage for virtual environments

Complete a brief survey to get a complimentary 70-page whitepaper featuring the best methods and solutions for your virtual environment, as well as hypervisor-specific management advice from TechTarget experts. Donâ€™t miss out on this exclusive content!

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States.
Privacy

Follow

Thanks! We'll email you when relevant content is added and updated.

## Discuss This Question: 10 Replies

REGISTER or login: