Thread: Logic problem: Base 6 count up/down

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    Logic problem: Base 6 count up/down

    Hello all.

    I have a logic problem with my implementation of a base-6 counter. The problem I have is that due to constraints in the system I am programming, I can only change a single digit at a time (much like a grey code).

    My implementation therefore counts first up, incrementing the next significant digit, then back down, incrementing the next significant digit, like this:

    000, 001, 002, 003, 004, 005,
    015, 014, 013, 012, 011, 010,
    020...

    My problem is that it works fine for two digits, but when I get to 050 - I am stuck with how to implement the logic to then increment the higher significant digit (to 150 and onwards) - it's driving me totally nuts.

    The current implementation has two 32 byte arrays, with each byte representing a digit of the counter. The first array is for the main counter digits; the second is for the direction table for each digit, so as to indicate if each byte is being incremented or decremented.

    Code:
    Code snippet:
    
        unsigned char dPOSITION[32];     // contains each digits value
        unsigned char iPOSITION[32];      // contais flag for direction (0=+/1=-)
    
        int j=0;
            
        for(i=0;i<MAX_COUNT;i++)       
        {
            j=0
            {
                if ( (dPOSITION[j]==5) && (iPOSITION[j]==0) )
                {
                    iPOSITION[j]=1;
                    j++;
                }
    
                if ( (dPOSITION[j]==0) && (iPOSITION[j]==1) )
                {
                    iPOSITION[j]=0;
                    j++;
                }
                if (iPOSITION[j]==0)        dPOSITION[j]++;
                if (iPOSITION[j]==1)        dPOSITION[j]--;
            }
        }
    Any help or pointers would be greatly appreciated.

    FF.
    Last edited by f0xf1re; 01-23-2008 at 12:36 AM. Reason: Spelling mistake

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Naturally, you could just make fifteen more copies of your two if-statements, to make sure that you can accommodate thirty-one "carries". You could.

    I would recommend abstracting the carry logic into a predicate function, like isAtEnd() or something, so that you can stick it in a while loop:
    Code:
    while (isAtEnd(dPosition, iPosition, j) {
        iPosition[j] = 1 - iPosition[j];  /* change 1 <-> 0 */
        j++;
    }
    This way you can get as many carries as you need without duplicated code or thirty-two nested if-statements or something even worse.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    After 050 you can simply put 150, and from then on until 100 the last two digits go through the reverse of what you just described. etc
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Quote Originally Posted by tabstop View Post
    Naturally, you could just make fifteen more copies of your two if-statements, to make sure that you can accommodate thirty-one "carries". You could.

    I would recommend abstracting the carry logic into a predicate function, like isAtEnd() or something, so that you can stick it in a while loop:
    Code:
    while (isAtEnd(dPosition, iPosition, j) {
        iPosition[j] = 1 - iPosition[j];  /* change 1 <-> 0 */
        j++;
    }
    This way you can get as many carries as you need without duplicated code or thirty-two nested if-statements or something even worse.

    Hello TabStop and many thanks for your fast reply!

    Yes, I considered the repeated IF statements, after all the frustration I admit it crossed my mind...

    I am not too good with C or English, so my understanding of "predicate" comes from Google, and this I take to mean a boolean true/false condition function, yes?

    I still can not get my head around how to implement it, though. Which logic did you mean to be placed into the IfAtEnd function? The increment/decrement portion?

    Sorry if I am being dumb!

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's what I mean by predicate, yes. So it has to be the true-false part: namely the (pair of) if-tests. (That is, it should return true if either of the if-tests would trigger, and false otherwise.)

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Quote Originally Posted by tabstop View Post
    That's what I mean by predicate, yes. So it has to be the true-false part: namely the (pair of) if-tests. (That is, it should return true if either of the if-tests would trigger, and false otherwise.)

    Ok, I have placed the code into the predicate function, but all it has done is shortened the code, it makes no difference to the logic?

    I still have to do the normal tests (with the IF statements) in order to determine if the new function should run, and it produces the same results as the previous version.

    What I do not understand is how this allows me to extends the functionality to itterate through the higher order digits?

    I do not correctly understand about the while loop you mention, I think you mean that I should maybe itterate through all values? If so, will this mean the changing of more than one value per itteration (which I can not do)?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The point is not that you're changing more than one value (note that no value gets changed inside the while loop, just directions!), it's that j must increase more than once to get from the "ones" digit to the digit you need to change (in this case of 050->150, the "hundreds" digit). And when you get to 505 -> 1505, j will have to increase three times, et cetera.

    Quote Originally Posted by f0xf1re
    What I do not understand is how this allows me to extends the functionality to itterate through the higher order digits?
    Trace it out. Suppose you're at 050, with up-up-down. You need to get to 150, with up-down-up. Or suppose you're at 0505 with up-up-down-up, you need to change to 1505 with up-down-up-down. The point of the while loop is that the changes will propagate from right to left all the way to the end of the number instead of at most once, which is what you had previously.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logic problem
    By MarlonDean in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2008, 05:11 AM
  2. Re : Logic problem
    By Wah in forum C Programming
    Replies: 5
    Last Post: 01-31-2002, 02:19 PM
  3. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM
  4. Base conversion problem!
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-08-2001, 07:00 PM