• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Arrays, For Loops,.....???

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I have a question on an assignment,(other than the cattle drive) and was hoping someone could give me a little shove.
The assignment wants me to write a program to print out every possible combination for a dial lock. The lock's dial range is 0-36 and the combinations must be three number sequences.
Can anyone give me a push in the right direction. Do I need to use an array, and/or for loops?
I just can't seem to grasp the concept of this.
Any help is greatly appreciated.
Thanks in advance!
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a set of 3 embedded loops.
In each loop use (int i = 0; 1 < 36 ; i++ )
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maths really !!!
You'll only need nested loops.
One question though, is 000 a valid lock combination? If not, change the first loop (outermost for loop) initialization from 0 to 1 (i=1).
Put this code in a suitable method (main???)
<pre>
int iCount = 1;
int iMax = 36;
String szLock = ""
for(i=0; i <= iMax; i++, iCount++)
{
szLock=i;
for(j=0; j <= iMax; j++,iCount++)
{
szLock += j;
for(k=0; k <= iMax; k++,iCount++)
{
szLock += k;
System.out.println("Combination #" + iCount + " is : " + szLock);
}
}
}
System.out.println("\n\nTotal Combinations = " + iCount - 1);
</pre>
BTW, that means lot of combinations!!!
HTH,
- Manish
[This message has been edited by Manish Hatwalne (edited October 03, 2001).]
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies. Manish, I am still new at Java, and I am struggling with learning the language. Perhaps if it is not too much trouble, could you document the code you displayed, to help me understand how this works??
Thanks so much
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manish,
If you use i <= 36; you will get 37 repetitions, zero being the first time through. Use i < 36;
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right,
I thought of that initially, but then I thought the range perhaps includes both the numbers (0 and 36). Jennifer, if 36 is not included as one of the numbers here, pls. make iMax as 35 or change the condition as Cindy says.
As for documentation, I am too lazy to do that. Perhaps, you can do that part of your assignment yourself.
OK, I'll try to explain what the code does -
Consider your code contaning 3 numbers (not digits), XYZ. Each can range from 0 to 36 (or 35). Each loop is generating these numbers.
Outermost (i loop) => X
Middle (j loop) => Y
Innermost (k loop) => Z
For each iteration of the outer loop the immediate inner loop executes 37 (or 36) times. A basic permutaion/combination question. Think of it in this way, how many 2 digit numbers you can have from digits 0 to 9 (both inclusive). It gives you -
XY as a combination, where X & Y both can take values in the range 0 to 9. You can have hundred such numbers (0 to 99).
I hope this make it easy for you to understand the code. In your case, the total combinatioons will be (37 * 37 * 37 = 50653 OR 36 * 36 * 36 = 46656).
HTH,
- Manish
p.s. - Cindy, thanks for pointing this. I wanted to include this assumption in my post but just forgot.
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys! It's friendly people like you who make learning Java almost bearable.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at the code below Jennifer. All you need is three nested (one inside the other) for loops, each controlled by 3 variables (i, j, and k), to represent the 3 dials on the lock. Each variable runs from 0 to 36. Execution will fall through the first 2 loops to the inner most one (k). This will loop to 36, and then execution will break out of this loop and back to the next most inner loop (j). j will get incremented to 1 and then execution will fall through to the k loop again. Variable k has now been incremented to 1, and it goes up to 36 as before, and then back out to j, which then runs from 1 to 36, and then back into k again which is now 2. So, k has to loop for 36 times before j gets incremented, and similarly, the outer most loop, i, has to wait until j is incremented to 36 (which takes 36 times 36). You can see this happening from the print to the screen. Hope this helps!
There's a lot of combinations to print, which will take some time. So you can just stop the execution when you like. Don't know why the question said 36, 10 would have done.

// This class prints an awful lot of combinations
// 36 * 36 * 36 = 46,656
public class Lock
{
public static void main (String args[])
{
System.out.println("Possible combinations for 3 times 36 lock");
System.out.println();
System.out.println("i\tj\tk");
System.out.println("-------------");

// nested loops for each dial
for(int i=0; i <= 36; i++)
{
for(int j=0; j <= 36; j++)
{
for(int k=0; k <= 36; k++)
{
System.out.println(i + "\t" + j + "\t" + k);
}
}
}
}
}
 
Eamonn Doherty
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, to improve this, instead of using 0 and 36 in the loops it would be better to have variables in there like so:
// This class prints an awful lot of combinations
// 36 * 36 * 36 = 46,656
public class Lock
{
public static void main (String args[])
{
int final MIN = 0;
int final MAX = 36;

System.out.println("Possible combinations for 3 times 36 lock");
System.out.println();
System.out.println("i\tj\tk");
System.out.println("-------------");

// nested loops for each dial
for(int i=MIN; i <= MAX; i++)
{
for(int j=MIN; j <= MAX; j++)
{
for(int k=MIN; k <= MAX; k++)
{
System.out.println(i + "\t" + j + "\t" + k);
}
}
}
}
}
So, if you need to change the number of combinations per dial you just change it in one place, where u declared and initalised the variables (MIN and MAX) at the start of the main method, and not in the loops as before. The variables can be declared final because they don't need to change. Also, because they are constants using capitals for their names is the convention.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic