Skip to main content

how to do this array program, anyone?

1 reply [Last post]
LT.SHANKS
Offline
Joined: 2012-04-22
Points: 0

here the question:

A permutation of three letters,a,b, and c is any arrangement of these letters in a row. For example, some of the permutation of these characters are abc, bca, and cab. the number of distinct permutations of three different characters is 6. design a solution where given any three letters,you could produce the six distinct permutation of those letters.

i'm still new in array.. so i may need anyone help on this.. =)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
2kjava
Offline
Joined: 2012-04-18
Points: 0

The easiest way for just 3 letters is by a 3 depth nested loop - see com3 below. The if statements are to make sure that patterns such as aab etc are rejected.

However, this way of doing it is no good if the number of letters is not known in advance or the number is large. In this case a recursive method is needed - see comn. This will allow all the permutations for max letters taking sel at a time. The main method allows these to be used from the command line. Note that I index column from 1 (not 0) to avoid a lot of - 1 in array access! I use column[0] as a temp for the exchange.

I hope this is of help and interest.

Ref - How to solve it by computer R.G. Dromey

package combi;

public class Combi {
private static int max, sel, noc;
private static int column[];

private static void exchange(int a, int b)
{
column[0] = column[a];
column[a] = column[b];
column[b] = column[0];
}

private static void output()
{
for (int i = 1; i <= sel; i++)
System.out.print((char)(column[i] + 'a'));
System.out.println();
}

private static void com3()
{
for (char c1 = 'a'; c1 <= 'c'; c1++)
for (char c2 = 'a'; c2 <= 'c'; c2++)
if (c1 != c2)
for (char c3 = 'a'; c3 <= 'c'; c3++)
if ((c3 != c1) && (c3 != c2)) {
noc++;
System.out.printf("%c%c%c\n", c1, c2, c3);
}
System.out.printf("There are %d combinations for 3 letters\n", noc);
}

private static void comn(int no)
{
for (int j = no; j <= max; j++) {
exchange(no, j);
if (no < sel)
comn(no + 1);
else {
output();
noc++;
}
exchange(no, j);
}
}

public static void main(String[] args) {
int noargs = args.length;

if (noargs > 2) {
System.out.println("Invalid number of arguments");
System.out.println("Use java [size] [time]");
System.out.println("Where [size] is the total number of characters");
System.out.println(" [time] is the number of characters to take at a time");
System.out.println("If no arguments are present then permuations for 3 characters are shown");
return;
}

noc = 0;
if (noargs == 0)
com3();
else {
max = Integer.parseInt(args[0]);
if ((max < 2) || (max > 26)) {
System.out.println("Number of characters is between 2 and 26");
return;
}

if (noargs == 2) {
sel = Integer.parseInt(args[1]);
if (sel > max)
sel = max;
} else
sel = max;

column = new int[max + 1];
for (int i = 1; i <= max; i++)
column[i] = i - 1;

comn(1);
System.out.printf("There are %d combinations for %d letters taken %d at a time\n", noc, max, sel);
}
}
}