Pattern Manipulation – Solution

This month’s Exercise was to create a cycle of numbers, values that repeat within a certain range. Of all the Exercises presented so far on this site, this one probably has the greatest number of solutions.

One of my first solutions used an array, which more inline with how animated graphics works: Each element in the array represents a color value for a specific panel in the display. Increment each of the panel’s values, then show the graphic. Repeat.

The key to making the entire process work is the modulus operator, %. You need to cap the incrementing values so that they don’t overflow. In this case, eight values are being presented, 0 through 7, so the function is x % 8.

It doesn’t matter how large an int value gets, the function x % 8 always caps the result to the range of 0 through 7. Even if a signed int value cycles around from 2147483647 to -2147483648 and back to zero, the result of x % 8 is always in the range 0 to 7.

My first solution below uses a while loop to cycle the values. The while(1) at Line 8 sets an infinite loop.

#include <stdio.h>

int main()
{
    int base,x;

    base = 0;
    while(1)
    {
        for(x=0;x<8;x++)
        {
            printf(" %d",(base+x)%8);
        }
        putchar('\n');
        base++;
    }

    return(0);
}

The value of base is incremented over and over (Line 15). Variable x provides an offset from that base in Line 12, which is how the values 0 through 7 are generated. That creates columnar data — a two-dimensional array — which could be used to cycle graphics.

Because I mentioned it in my challenge, the following code does the same thing, but uses only one variable:

#include <stdio.h>

int main()
{
    int base = 0;

    while(1)
    {
        printf("%d %d %d %d %d %d %d %d\n",
            base%8,
            (base+1)%8,
            (base+2)%8,
            (base+3)%8,
            (base+4)%8,
            (base+5)%8,
            (base+6)%8,
            (base+7)%8);
        base++;
    }

    return(0);
}

While the above solution is possible, and it’s neatly formatted, it’s not very elegant programming. Any time I see a series of values +1, +2, +3, and so on, I question why the programmer didn’t use a loop. In this case, it’s simply to prove that a one-variable solution is possible.

3 thoughts on “Pattern Manipulation – Solution

  1. #include

    int main()
    {
    int x=0;

    while(1)
    {
    printf(“%d “,x++%8);
    printf(“%d “,x++%8);
    printf(“%d “,x++%8);
    printf(“%d “,x++%8);
    printf(“%d “,x++%8);
    printf(“%d “,x++%8);
    printf(“%d “,x++%8);
    printf(“%d\n”,x++%8);
    ++x%8;
    }

    return(0);
    }

    This was my solution. Looking at your solution, I realise now I didn’t need the mod on the ++x as int x would wrap around when it reached its limits.

  2. A very valid solution, coder.

    I’m going to add a page (now that comments are re-enabled) offering tips on uploading code:

    Use the <pre> tag to mark the start of your code. Use the </pre> tag to flag the end. That way the indents and spacing are retained.

    Also, you need to use the &lt; for the less-than symbol, and &gt; for greater-than. Otherwise, the browser interprets something like <stdio.h> as an HTML code.

    Thanks for your contribution!

  3. #include <stdio.h>

    int main() {
    int x=0;

    while(1) {
    printf(“%d “,x++%8);
    printf(“%d “,x++%8);
    printf(“%d “,x++%8);
    printf(“%d “,x++%8);
    printf(“%d “,x++%8);
    printf(“%d “,x++%8);
    printf(“%d “,x++%8);
    printf(“%d\n”,x++%8);
    ++x%8;
    }

    return(0);
    }

    A better attempt at showing my solution 🙂

Leave a Reply