Programming pointers!

Discussion in 'Mac Apps and Mac App Store' started by mmmdreg, Mar 28, 2004.

  1. mmmdreg macrumors 65816

    mmmdreg

    Joined:
    Apr 14, 2002
    Location:
    Sydney, Australia
    #1
    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!
     
  2. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #2
    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.
     
  3. oldschool macrumors 65816

    oldschool

    Joined:
    Sep 30, 2003
    #3
    And i was considering switching from biology to computer science. What was i thinking!

    :D
     
  4. mmmdreg thread starter macrumors 65816

    mmmdreg

    Joined:
    Apr 14, 2002
    Location:
    Sydney, Australia
    #4
    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.
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    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.
     

Share This Page