Java N-ary Counter

Discussion in 'Java' started by Hanky Sjafrie, Sep 19, 2003.

  1. Greetings,

    I need help on implementing a N-ary counter, which iterates from zero
    to the maximum value allowed for each column. The maximum value of
    n-th column should corresponds to the n-th value of the input array,
    for example:

    int[] inputArray ={2,1,3};

    The output should be stored in a double array:
    int[][] outputArray = new int[2*1*3][inputArray.length];

    which has the values like these:
    outputArray[0][] = {0,0,0}
    outputArray[1][] = {0,0,1}
    outputArray[2][] = {0,0,2}
    outputArray[3][] = {1,0,0}
    outputArray[4][] = {1,0,1}
    outputArray[5][] = {1,0,2}

    Probably a recursive will do, but I'm still out of luck... :-(
    Any help would be greatly appreciated. Thanks in advance!

    Regards,

    Hanky
     
    Hanky Sjafrie, Sep 19, 2003
    #1
    1. Advertisements

  2. Hanky Sjafrie

    pete kirkham Guest

    Hanky Sjafrie wrote:
    > I need help on implementing a N-ary counter


    > The output should be stored in a double array:

    By which you mean an integer array of rank 2.

    Assuming this is probably homework, you need to look at the 'modulus'
    operator % (strictly the remainder operator, as the behaviour differs
    for negative integers), and think carrying the overflow from one digit
    to the next. That can be done quite easily iteratively, by a loop that
    starts at the least significant digit and increments until the most
    sig'nt digit is reached, or of course recursively if you really want to.


    Pete
     
    pete kirkham, Sep 20, 2003
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. The Eeediot
    Replies:
    3
    Views:
    2,506
    =?Utf-8?B?UnVsaW4gSG9uZw==?=
    Dec 22, 2004
  2. Wynan James

    Depth of n-ary tree

    Wynan James, Sep 29, 2003, in forum: Java
    Replies:
    10
    Views:
    12,180
    Stefan Poehn
    Sep 30, 2003
  3. Stevey

    N-ary tree code?

    Stevey, Apr 9, 2005, in forum: Java
    Replies:
    1
    Views:
    3,905
    Abhijat Vatsyayan
    Apr 10, 2005
  4. Tom Chance

    Plotting n-ary trees

    Tom Chance, Jan 18, 2004, in forum: Python
    Replies:
    0
    Views:
    386
    Tom Chance
    Jan 18, 2004
  5. n-ary tree

    , Oct 22, 2006, in forum: C++
    Replies:
    1
    Views:
    1,297
    Daniel T.
    Oct 22, 2006
  6. George2
    Replies:
    1
    Views:
    1,015
    Alf P. Steinbach
    Jan 31, 2008
  7. Replies:
    4
    Views:
    1,138
    Bo Persson
    Apr 14, 2008
  8. Guilfoy9898

    M-ary Tree

    Guilfoy9898, Jul 25, 2008, in forum: C Programming
    Replies:
    0
    Views:
    532
    Guilfoy9898
    Jul 25, 2008
Loading...