mardi 6 février 2018

Vertical Zig-Zag Pattern Printing using C

I wrote a function to print the below pattern. For example, if the n value is 4 the pattern is

1
2 7
3 6 8
4 5 9 10

Or if the value of n is 5, then the pattern is

1
2 9
3 8 10
4 7 11 14
5 6 12 13 15

My function gives me the first two block but not the next block. I'm stuck here for long time!

My function is

int printPattern(int n) {
    int row, column, fwdCtr = 1, evenCtr = 0, ctr = n;
    for(row = 1; row <= n; row++) {
        fwdCtr = row;
        for(column = 1; column <= row; column++) {
            if(column % 2 != 0) {
                printf("%d ", fwdCtr++);
            } else {
                evenCtr = fwdCtr + ctr;
                printf("%d ", evenCtr);
                ctr = ctr - 2;
            }
        }
        printf("\n");
    }     
}

Please give suggestions of changes!

Aucun commentaire:

Enregistrer un commentaire