# A For loop that calculates every possible outcome for 2 AND 3 chars...

• October 23rd, 2012, 06:24 PM
MyNamesMatt
A For loop that calculates every possible outcome for 2 AND 3 chars...
Hi there,

I'm having some trouble with an assignment.

I'm having a slight problem creating a nested for loop that displays every possible combination for random input. It's supposed to be like a slot machine game. The person is to enter three characters, it checks the characters, stores them, and then is supposed to give every combo of the chars like so...

Input = @ and #

@@@
@@#
@##
@#@
###
##@
#@@
#@#

How could I create a nested for loop that does so.

I can't even think of how to do it on paper. Apparently it requires 3 nested for loops, one for each character.

Input can also be three chars such as @#\$, Which would yield 27 possibilities instead of 8.

Here is my code so far....

I know how to do a nested for loop, and I know how they work... but I don't understand how you can create every possible solution, regardless of what is entered...

Code :

``` import javax.swing.*; import java.util.*;     public class oneArmBandit { public static void main (String [] args) { //variables String input = "full"; String empty = ""; boolean condition = true; Scanner keyboard; char signOne = '-'; char signTwo = '-'; char signThree = '-'; int inputLength = 0; int numberOfElements = 0;   //for(int i = 0; i < numberOfElements; i++)   /* for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { System.out.println("i: " +i+ " j: " +j); } } */         while (!input.equals(empty) && condition == true) { input = JOptionPane.showInputDialog("Enter the symbols on the wheels:"); keyboard = new Scanner(input);   System.out.println(input + "\n"); inputLength = (input.length());     if (inputLength == 2) { signOne = (input).charAt(0); signTwo = (input).charAt(1);   for(int i = 0; i < inputLength; i++)   for(int j = 0; j < 2; j++) {System.out.println(signOne); System.out.println(signTwo);};   for (int k = 0; k < 2; k++) {   condition = false; } }               }   } }       /* if (inputLength == 3) { signOne = (input).charAt(0); signTwo = (input).charAt(1); signThree = (input).charAt(2);   for(int i = 0; i < inputLength; i++) { System.out.println(signOne);   for(int j = 0; j < inputLength; j++) { System.out.println(signTwo);   for(int k = 0; k < numberOfElements; k++) { System.out.println(signThree);   } }   condition = false; }       */```

For my code, I'm just trying to get the for loop to run when the char's entered are only two... That's why the bottom portion, when char == 3, is commented out...

I can't seem to think of a solution/what to put in the for loop to make it run all possible combinations.

I am new to programming, so laymen's terms would be appreciated b-(

Cheers,

Matt
• October 23rd, 2012, 08:10 PM
jps
Re: A For loop that calculates every possible outcome for 2 AND 3 chars...
Quote:

Input = @ and #
I can\'t even think of how to do it on paper. Apparently it requires 3 nested for loops, one for each character.
I see two characters. Now is it missing a character or do you want two loops? If you don\'t have a solution to the problem you should not be writing code yet. Work on steps to solve the problem first and see how far you can get. It truly is helpful in writing code.
• October 23rd, 2012, 09:16 PM
MyNamesMatt
Re: A For loop that calculates every possible outcome for 2 AND 3 chars...
Quote:

Originally Posted by jps
I see two characters. Now is it missing a character or do you want two loops? If you don't have a solution to the problem you should not be writing code yet. Work on steps to solve the problem first and see how far you can get. It truly is helpful in writing code.

Since it is supposed to be like a slot machine, it is supposed to display 3 characters, even if 2 are inputted, such as;

Input = @ and #

@@@
@@#
@#@
@##
###
##@
#@@
#@#

....

and if input is # @ \$

it displays the same sort of deal but there are a lot more outcomes obviously...

I understand the concept, using a nested FOR loop to create this output, but what is supposed to put into the...

for (???; ???; ???)
for (???; ???; ???)
for(???;???;???)

:(
• October 23rd, 2012, 11:22 PM
jps
Re: A For loop that calculates every possible outcome for 2 AND 3 chars...
Did you write down steps to take to solve the problem?

What are they? What step are you stuck on?

You want to print "one thing" three times. Where that "one thing" is a choice of an unknown number of items. So you need some way to get and save an unknown number of items. Some way to (randomly?) choose one element from the set of options. Having these steps in order will make code writing much easier.
• October 23rd, 2012, 11:22 PM
Mugambo
Instead of using a for loop, try to populate a ch[3] with random symbols appearing from a list of symbols in each of the array position.
• October 24th, 2012, 10:59 AM
Zaphod_b
Re: A For loop that calculates every possible outcome for 2 AND 3 chars...
Very early in every beginning computer class they cover number systems, right?
Positional number systems with base 10: Each digit can have a value of 0, 1, ... 9

How many decimal numbers are there with three digits? (Answer 10 to the power 3, or 1000);

How about base 2? Surely they covered that. Each binary digit can have a value of 0, 1
How many binary numbers are there with three binary digits? (Fill in the blank here: ___________)

They might not have covered base 3 specifically, but it is an obvious extension: Each ternary digit can have a value of 0, 1, 2
How many numbers with base 3 are there with three ternary digits? (Fill in the blank here: ___________)

Ho-hum, right? I wasn't in your class, but I can see it plainly: All of those eyes rolling upward. I can even read all of those thoughts: "So what? When do we get to the Good Stuff? I hope we get out of class soon. Won't that bell ever ring?"

Well, here's an application (that is very simple to implement) using number systems like those that were covered:

Think of your display as a number. We will display some symbols representing the digits of a number.

If there are three positions, think of it as a three-digit number. If there are two possible symbols then it is a binary number.

First question: How many different patterns are there? (Answer : 2 to the power 3). Surely they covered stuff like this.

Next question: How can I use my vast knowledge of number systems to display all possible patterns?

Well, you can have a counter that goes from 0 through 7. Just display the binary digits of each count. Or, if we wanted symbols, say '@' and '#' just let '@' represent binary 0 and let '#' represent binary 1

I like to visualize the output before writing the program, and my program output might look like the following:
Code :

```Number of positions = 3 Number of symbols = 2 (@ #)   Number of patterns = 8   N Base 2 Display ----------------------- 0 0 0 0 (@ @ @) 1 0 0 1 (@ @ #) 2 0 1 0 (@ # @) 3 0 1 1 (@ # #) 4 1 0 0 (# @ @) 5 1 0 1 (# @ #) 6 1 1 0 (# # @) 7 1 1 1 (# # #)```

After debugging, I would remove superfluous output and just show whatever the assignment requires.

Now, what if you wanted three symbols, '@', '#', '\$'

So now think of the number that leads to the display is a number in base 3. (Each digit can have value 0, 1, 2)

The number of different displays is equal to (3 to the power 3) = 27

I would make a counter that goes from zero through 26 and show the base-3 digits for each value and the symbols for those digits:

Then my program output might look like this:
Code :

```Number of positions = 3 Number of symbols = 3 (@ # \$)   Number of patterns = 27   N Base 3 Display ----------------------- 0 0 0 0 (@ @ @) 1 0 0 1 (@ @ #) 2 0 0 2 (@ @ \$) 3 0 1 0 (@ # @) 4 0 1 1 (@ # #) 5 0 1 2 (@ # \$) 6 0 2 0 (@ \$ @) 7 0 2 1 (@ \$ #) 8 0 2 2 (@ \$ \$) 9 1 0 0 (# @ @) 10 1 0 1 (# @ #) 11 1 0 2 (# @ \$) 12 1 1 0 (# # @) 13 1 1 1 (# # #) 14 1 1 2 (# # \$) 15 1 2 0 (# \$ @) 16 1 2 1 (# \$ #) 17 1 2 2 (# \$ \$) 18 2 0 0 (\$ @ @) 19 2 0 1 (\$ @ #) 20 2 0 2 (\$ @ \$) 21 2 1 0 (\$ # @) 22 2 1 1 (\$ # #) 23 2 1 2 (\$ # \$) 24 2 2 0 (\$ \$ @) 25 2 2 1 (\$ \$ #) 26 2 2 2 (\$ \$ \$)```

Summary:

The thing can be done for any number of positions and any number of symbols. It will result in a finite number of deterministic operations.

If this approach appeals to you, think of how the program will work:

A single loop for the counter. If there are N positions and S symbols, the loop limit (number of patterns) is

S to the power N

For each value of the counter, derive digits in base S . For each count value you can store the Base-S digits in an array (size N) if you want to. You can use the digits to index into an array of characters (or Strings) that holds the symbols corresponding to the digits. Print out the symbols, whatever...

Then, it's time to start writing code. Implementation is a snap.

Cheers!

Z