Question

I am writing some functionality for a visual node based CAD program that will not allow for me to loop so I need a workaround to enumerate a list of numbers. I am an architect with very little programming experience so any help would be great.

A have an array of numbers(numArray) coming in as such 0,1,2,3,4... (first column) I need to take those numbers and convert them into their counterpart for column 1,2,3,4 without using any loops or nested loops.

   numArray 1 2 3 4
        -----------
        0 = 0|0|0|0
        1 = 0|0|0|1
        2 = 0|0|0|2
        3 = 0|0|0|3
        4 = 0|0|1|0
        5 = 0|0|1|1
        6 = 0|0|1|2
        7 = 0|0|1|3
        8 = 0|0|2|0
        9 = 0|0|2|1
        10= 0|0|2|2
        12= 0|0|2|3
        13= 0|0|3|0
        14= 0|0|3|1
        15= 0|0|3|2
        16= 0|1|3|3
        17= 0|1|0|0
        18= 0|1|0|1
        19= 0|1|0|2
        20= 0|1|0|3
        21= 0|1|1|0
        22= 0|1|1|1
        23= 0|1|1|2
        24= 0|1|1|3

I have figured out column 4 by implementing the following:

int column4 = numArray % 4;

this works and creates the numbers as such 0,1,2,3,0,1,2,3.... this is great however I am not sure how to use the num array coming in to produce column 3 2 and 1. Again I have very little programming experience so any help would be great.

Was it helpful?

Solution

You're converting the input to base 4 notation, so this will do the job:

int input[] = {0,1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20,21,22,23,24};

for (int i = 0; i < sizeof(input)/sizeof(input[0]); i++)
{
    int c1, c2, c3, c4;

    c4 = input[i] % 4;
    c3 = (input[i] / 4) % 4;
    c2 = (input[i] / 16) % 4;
    c1 = (input[i] / 64) % 4;

    printf("%d = %d\t%d\t%d\t%d\n", input[i], c1, c2, c3, c4);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top