Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

How to make combination function generic

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have written a function which generates combination in a desired order. The code is somthing like this



The output of this code is and this is what is required :



My problem is all the hard coding in that function i am passing "min" "max" for range of digits and this part is ok (more or less)
Please ignore the Boolean value.

"noOfControls" is something i need to implement ie all the combination i am getting are 3 digits max (governed by number of for loop).

I need to remove this dependency i don't want to write new method every time say "if i need series upto 6 digits and write method with 6 loops"

My question is what should be the approach ?
and what are the possible ways to do it.

Thanks in advance.
 
Sheriff
Posts: 22755
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out recursion. You create a private method that takes the number of elements to add. When initially called with 6, you take one element, then call the same method but with 5 this time. Continue until the count is 1.
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic