Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mmmdreg

macrumors 65816
Original poster
Apr 14, 2002
1,393
0
Sydney, Australia
Say I have a two dimensional array. One dimesion with 7 possible values (Y), the other with "X" (any positive integer). Say I want to write to a file every possible combination the X and Y, where every item X has to be used while Y (1-7) may have any amount used. (Imagine a poker machine with "X" number of wheels, with 7 items on each wheel).

How do I go about writing an algorithm for that? The writing is trivial but the actual going through of combinations. Because I can only seem to manage when X is a known constant..

Use whatever language (including Pseudo or plain english) if you can help me!
 
Assuming your array is formed like:

myArray[Y][X]

you should be able to do something like:

Code:
for (int Y = 1; Y <= 7; Y++) {
     for (int X = 1; X < myArray[Y].length; X++) {
          Print myArray[Y][X];
     }
}

Of course it depends on if your array starts at 0 or 1 and such but that's the gist of it.

If your array is:

myArray[X][Y]

then you'd do:

Code:
for (int X = 1; X < myArray.length; X++) {
     for (int Y = 1; Y <= 7; Y++) {
          Print myArray[X][Y];
     }
}

In Java you'd use myArray.length, in VB you'd use UBound(myArray)... ermmm... can't remember the other ones... but in general that'll getcha done I think.
 
And i was considering switching from biology to computer science. What was i thinking!

SilentPanda said:
Assuming your array is formed like:

myArray[Y][X]

you should be able to do something like:

Code:
for (int Y = 1; Y <= 7; Y++) {
     for (int X = 1; X < myArray[Y].length; X++) {
          Print myArray[Y][X];
     }
}

Of course it depends on if your array starts at 0 or 1 and such but that's the gist of it.

If your array is:

myArray[X][Y]

then you'd do:

Code:
for (int X = 1; X < myArray.length; X++) {
     for (int Y = 1; Y <= 7; Y++) {
          Print myArray[X][Y];
     }
}

In Java you'd use myArray.length, in VB you'd use UBound(myArray)... ermmm... can't remember the other ones... but in general that'll getcha done I think.
:D
 
thankyou but just from looking at that, wouldn't that write 1 X and Y value only? What I meant was that the printed message will be the position of every single X value in that combo. ie. Every X value must be used but not every Y in each round.
 
mmmdreg said:
thankyou but just from looking at that, wouldn't that write 1 X and Y value only? What I meant was that the printed message will be the position of every single X value in that combo. ie. Every X value must be used but not every Y in each round.

The entire cartesian product will be printed, one element at a time. There is not difference to the user if you print the entire product at once, or one element at a time (unless of course your environment adds a new line to each print). If you each to print each X for a given Y at a time instead of Y for a given X then simply swap the inner loop for the outer loop.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.