Solution for Exercise 10-13

ex1013

#include <stdio.h>

#define GRID 3

/* prototypes */
void forward(void);
void backwards(void);

int main()
{
    puts("Grid forward:");
    forward();
    puts("Grid backwards:");
    backwards();
    return(0);
}

void forward(void)
{
    int x,y;

    for(x=0;x<GRID;x++)
    {
        for(y=0;y<GRID;y++)
            printf("%d:%d\t",x,y);
        putchar('\n');
    }
}

void backwards(void)
{
    int x,y;

    for(x=GRID-1;x>=0;x--)
    {
        for(y=GRID-1;y>=0;y--)
            printf("%d:%d\t",x,y);
        putchar('\n');
    }
}

Output

Grid forward:
0:0 0:1 0:2
1:0 1:1 1:2
2:0 2:1 2:2
Grid backwards:
2:2 2:1 2:0
1:2 1:1 1:0
0:2 0:1 0:0

Notes

* You can get into trouble when you misspell a defined constant, though the compiler is very good about pointing out such errors.