cspiral

How to draw a spiral of # with only for/while loops and if/else statements (without arrays) using C


so I'm very new to C and am wondering how I could create the following spiral shape: enter image description here

This is my comical attempt..

#include <stdio.h>
int main(void) {
    int size;
    int i,j;

    printf("Enter size: ");
    scanf("%d", &size);

    for (i = 0, j = 0; i < size - 4; i++, j+=2) {

        int row = 1;
        while (row <= size) {
            int column = 1; 
            while (column <= size) {

                if ((row == 1 + j && column > j && column < size - j)|| 
                    (row == size - (j) && column > j && column < size - j) || 
                    (column == size - (j) && row > j && row <= size - j) ||
                    (column == 1 + j && row > (2+j) && row < size - j)) {
                    printf("*");
                }

                else {
                    printf("-");
                }

                column++;
            }   
            row++;
            printf("\n");

        }
    }
    return 0;
}

Any help would be greatly appreciated!


Solution

  • I enjoyed this problem and ran into some troubles coming up with an approach. Enough messing around makes the pattern obvious, though; here's the result:

    #include <stdio.h>
    
    #define BLANK  "  "
    #define FILLED "# "
    
    void spiral(int size) {
        int i, j;
    
        if (!(size & 1)) { size++; }
        
        int half = size / 2 + 1;
    
        /* top */
        
        for (i = 0; i < half; i++) {
            if (i & 1) {
                for (j = 0; j < size; j++) {
                    if (!(j & 1) && (j < i - 1 || j >= size - i)) {
                        printf(FILLED);
                    }
                    else {
                        printf(BLANK);
                    }
                }
            }
            else {
                for (j = 0; j < size; j++) {
                    if ((j & 1) && (j < i - 1 || j >= size - i)) {
                        printf(BLANK);
                    }
                    else {
                        printf(FILLED);
                    }
                }
            }
    
            puts("");
        }
        
        /* bottom half */
    
        for (; i < size; i++) {
            if (i & 1) {
                for (j = 0; j < size; j++) {
                    if (!(j & 1) && (j < size - i || j > i)) {
                        printf(FILLED);
                    }
                    else {
                        printf(BLANK);
                    }
                }
            }
            else {
                for (j = 0; j < size; j++) {
                    if ((j & 1) && (j < size - i || j > i)) {
                        printf(BLANK);
                    }
                    else {
                        printf(FILLED);
                    }
                }
            }
    
            puts("");
        }
    }
    
    int main() {
        int i;
    
        for (i = 1; i < 35; i += 2 ) {
            spiral(i);
            puts("\n");
        }
    
        return 0;
    }
    

    Output:

    # 
    
    
    # # # 
        # 
    # # # 
    
    
    # # # # # 
            # 
    # # #   # 
    #       # 
    # # # # # 
    
    
    # # # # # # # 
                # 
    # # # # #   # 
    #       #   # 
    #   # # #   # 
    #           # 
    # # # # # # # 
    
    
    # # # # # # # # # 
                    # 
    # # # # # # #   # 
    #           #   # 
    #   # # #   #   # 
    #   #       #   # 
    #   # # # # #   # 
    #               # 
    # # # # # # # # # 
    
    
    # # # # # # # # # # # 
                        # 
    # # # # # # # # #   # 
    #               #   # 
    #   # # # # #   #   # 
    #   #       #   #   # 
    #   #   # # #   #   # 
    #   #           #   # 
    #   # # # # # # #   # 
    #                   # 
    # # # # # # # # # # # 
    
    
    # # # # # # # # # # # # # 
                            # 
    # # # # # # # # # # #   # 
    #                   #   # 
    #   # # # # # # #   #   # 
    #   #           #   #   # 
    #   #   # # #   #   #   # 
    #   #   #       #   #   # 
    #   #   # # # # #   #   # 
    #   #               #   # 
    #   # # # # # # # # #   # 
    #                       # 
    # # # # # # # # # # # # # 
    
    
    # # # # # # # # # # # # # # # 
                                # 
    # # # # # # # # # # # # #   # 
    #                       #   # 
    #   # # # # # # # # #   #   # 
    #   #               #   #   # 
    #   #   # # # # #   #   #   # 
    #   #   #       #   #   #   # 
    #   #   #   # # #   #   #   # 
    #   #   #           #   #   # 
    #   #   # # # # # # #   #   # 
    #   #                   #   # 
    #   # # # # # # # # # # #   # 
    #                           # 
    # # # # # # # # # # # # # # # 
    
    
    # # # # # # # # # # # # # # # # # 
                                    # 
    # # # # # # # # # # # # # # #   # 
    #                           #   # 
    #   # # # # # # # # # # #   #   # 
    #   #                   #   #   # 
    #   #   # # # # # # #   #   #   # 
    #   #   #           #   #   #   # 
    #   #   #   # # #   #   #   #   # 
    #   #   #   #       #   #   #   # 
    #   #   #   # # # # #   #   #   # 
    #   #   #               #   #   # 
    #   #   # # # # # # # # #   #   # 
    #   #                       #   # 
    #   # # # # # # # # # # # # #   # 
    #                               # 
    # # # # # # # # # # # # # # # # # 
    
    
    # # # # # # # # # # # # # # # # # # # 
                                        # 
    # # # # # # # # # # # # # # # # #   # 
    #                               #   # 
    #   # # # # # # # # # # # # #   #   # 
    #   #                       #   #   # 
    #   #   # # # # # # # # #   #   #   # 
    #   #   #               #   #   #   # 
    #   #   #   # # # # #   #   #   #   # 
    #   #   #   #       #   #   #   #   # 
    #   #   #   #   # # #   #   #   #   # 
    #   #   #   #           #   #   #   # 
    #   #   #   # # # # # # #   #   #   # 
    #   #   #                   #   #   # 
    #   #   # # # # # # # # # # #   #   # 
    #   #                           #   # 
    #   # # # # # # # # # # # # # # #   # 
    #                                   # 
    # # # # # # # # # # # # # # # # # # # 
    
    
    # # # # # # # # # # # # # # # # # # # # # 
                                            # 
    # # # # # # # # # # # # # # # # # # #   # 
    #                                   #   # 
    #   # # # # # # # # # # # # # # #   #   # 
    #   #                           #   #   # 
    #   #   # # # # # # # # # # #   #   #   # 
    #   #   #                   #   #   #   # 
    #   #   #   # # # # # # #   #   #   #   # 
    #   #   #   #           #   #   #   #   # 
    #   #   #   #   # # #   #   #   #   #   # 
    #   #   #   #   #       #   #   #   #   # 
    #   #   #   #   # # # # #   #   #   #   # 
    #   #   #   #               #   #   #   # 
    #   #   #   # # # # # # # # #   #   #   # 
    #   #   #                       #   #   # 
    #   #   # # # # # # # # # # # # #   #   # 
    #   #                               #   # 
    #   # # # # # # # # # # # # # # # # #   # 
    #                                       # 
    # # # # # # # # # # # # # # # # # # # # # 
    
    
    # # # # # # # # # # # # # # # # # # # # # # # 
                                                # 
    # # # # # # # # # # # # # # # # # # # # #   # 
    #                                       #   # 
    #   # # # # # # # # # # # # # # # # #   #   # 
    #   #                               #   #   # 
    #   #   # # # # # # # # # # # # #   #   #   # 
    #   #   #                       #   #   #   # 
    #   #   #   # # # # # # # # #   #   #   #   # 
    #   #   #   #               #   #   #   #   # 
    #   #   #   #   # # # # #   #   #   #   #   # 
    #   #   #   #   #       #   #   #   #   #   # 
    #   #   #   #   #   # # #   #   #   #   #   # 
    #   #   #   #   #           #   #   #   #   # 
    #   #   #   #   # # # # # # #   #   #   #   # 
    #   #   #   #                   #   #   #   # 
    #   #   #   # # # # # # # # # # #   #   #   # 
    #   #   #                           #   #   # 
    #   #   # # # # # # # # # # # # # # #   #   # 
    #   #                                   #   # 
    #   # # # # # # # # # # # # # # # # # # #   # 
    #                                           # 
    # # # # # # # # # # # # # # # # # # # # # # # 
    
    
    # # # # # # # # # # # # # # # # # # # # # # # # # 
                                                    # 
    # # # # # # # # # # # # # # # # # # # # # # #   # 
    #                                           #   # 
    #   # # # # # # # # # # # # # # # # # # #   #   # 
    #   #                                   #   #   # 
    #   #   # # # # # # # # # # # # # # #   #   #   # 
    #   #   #                           #   #   #   # 
    #   #   #   # # # # # # # # # # #   #   #   #   # 
    #   #   #   #                   #   #   #   #   # 
    #   #   #   #   # # # # # # #   #   #   #   #   # 
    #   #   #   #   #           #   #   #   #   #   # 
    #   #   #   #   #   # # #   #   #   #   #   #   # 
    #   #   #   #   #   #       #   #   #   #   #   # 
    #   #   #   #   #   # # # # #   #   #   #   #   # 
    #   #   #   #   #               #   #   #   #   # 
    #   #   #   #   # # # # # # # # #   #   #   #   # 
    #   #   #   #                       #   #   #   # 
    #   #   #   # # # # # # # # # # # # #   #   #   # 
    #   #   #                               #   #   # 
    #   #   # # # # # # # # # # # # # # # # #   #   # 
    #   #                                       #   # 
    #   # # # # # # # # # # # # # # # # # # # # #   # 
    #                                               # 
    # # # # # # # # # # # # # # # # # # # # # # # # # 
    
    
    # # # # # # # # # # # # # # # # # # # # # # # # # # # 
                                                        # 
    # # # # # # # # # # # # # # # # # # # # # # # # #   # 
    #                                               #   # 
    #   # # # # # # # # # # # # # # # # # # # # #   #   # 
    #   #                                       #   #   # 
    #   #   # # # # # # # # # # # # # # # # #   #   #   # 
    #   #   #                               #   #   #   # 
    #   #   #   # # # # # # # # # # # # #   #   #   #   # 
    #   #   #   #                       #   #   #   #   # 
    #   #   #   #   # # # # # # # # #   #   #   #   #   # 
    #   #   #   #   #               #   #   #   #   #   # 
    #   #   #   #   #   # # # # #   #   #   #   #   #   # 
    #   #   #   #   #   #       #   #   #   #   #   #   # 
    #   #   #   #   #   #   # # #   #   #   #   #   #   # 
    #   #   #   #   #   #           #   #   #   #   #   # 
    #   #   #   #   #   # # # # # # #   #   #   #   #   # 
    #   #   #   #   #                   #   #   #   #   # 
    #   #   #   #   # # # # # # # # # # #   #   #   #   # 
    #   #   #   #                           #   #   #   # 
    #   #   #   # # # # # # # # # # # # # # #   #   #   # 
    #   #   #                                   #   #   # 
    #   #   # # # # # # # # # # # # # # # # # # #   #   # 
    #   #                                           #   # 
    #   # # # # # # # # # # # # # # # # # # # # # # #   # 
    #                                                   # 
    # # # # # # # # # # # # # # # # # # # # # # # # # # # 
    
    
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
                                                            # 
    # # # # # # # # # # # # # # # # # # # # # # # # # # #   # 
    #                                                   #   # 
    #   # # # # # # # # # # # # # # # # # # # # # # #   #   # 
    #   #                                           #   #   # 
    #   #   # # # # # # # # # # # # # # # # # # #   #   #   # 
    #   #   #                                   #   #   #   # 
    #   #   #   # # # # # # # # # # # # # # #   #   #   #   # 
    #   #   #   #                           #   #   #   #   # 
    #   #   #   #   # # # # # # # # # # #   #   #   #   #   # 
    #   #   #   #   #                   #   #   #   #   #   # 
    #   #   #   #   #   # # # # # # #   #   #   #   #   #   # 
    #   #   #   #   #   #           #   #   #   #   #   #   # 
    #   #   #   #   #   #   # # #   #   #   #   #   #   #   # 
    #   #   #   #   #   #   #       #   #   #   #   #   #   # 
    #   #   #   #   #   #   # # # # #   #   #   #   #   #   # 
    #   #   #   #   #   #               #   #   #   #   #   # 
    #   #   #   #   #   # # # # # # # # #   #   #   #   #   # 
    #   #   #   #   #                       #   #   #   #   # 
    #   #   #   #   # # # # # # # # # # # # #   #   #   #   # 
    #   #   #   #                               #   #   #   # 
    #   #   #   # # # # # # # # # # # # # # # # #   #   #   # 
    #   #   #                                       #   #   # 
    #   #   # # # # # # # # # # # # # # # # # # # # #   #   # 
    #   #                                               #   # 
    #   # # # # # # # # # # # # # # # # # # # # # # # # #   # 
    #                                                       # 
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
    
    
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
                                                                # 
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # #   # 
    #                                                       #   # 
    #   # # # # # # # # # # # # # # # # # # # # # # # # #   #   # 
    #   #                                               #   #   # 
    #   #   # # # # # # # # # # # # # # # # # # # # #   #   #   # 
    #   #   #                                       #   #   #   # 
    #   #   #   # # # # # # # # # # # # # # # # #   #   #   #   # 
    #   #   #   #                               #   #   #   #   # 
    #   #   #   #   # # # # # # # # # # # # #   #   #   #   #   # 
    #   #   #   #   #                       #   #   #   #   #   # 
    #   #   #   #   #   # # # # # # # # #   #   #   #   #   #   # 
    #   #   #   #   #   #               #   #   #   #   #   #   # 
    #   #   #   #   #   #   # # # # #   #   #   #   #   #   #   # 
    #   #   #   #   #   #   #       #   #   #   #   #   #   #   # 
    #   #   #   #   #   #   #   # # #   #   #   #   #   #   #   # 
    #   #   #   #   #   #   #           #   #   #   #   #   #   # 
    #   #   #   #   #   #   # # # # # # #   #   #   #   #   #   # 
    #   #   #   #   #   #                   #   #   #   #   #   # 
    #   #   #   #   #   # # # # # # # # # # #   #   #   #   #   # 
    #   #   #   #   #                           #   #   #   #   # 
    #   #   #   #   # # # # # # # # # # # # # # #   #   #   #   # 
    #   #   #   #                                   #   #   #   # 
    #   #   #   # # # # # # # # # # # # # # # # # # #   #   #   # 
    #   #   #                                           #   #   # 
    #   #   # # # # # # # # # # # # # # # # # # # # # # #   #   # 
    #   #                                                   #   # 
    #   # # # # # # # # # # # # # # # # # # # # # # # # # # #   # 
    #                                                           # 
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
    
    
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
                                                                    # 
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #   # 
    #                                                           #   # 
    #   # # # # # # # # # # # # # # # # # # # # # # # # # # #   #   # 
    #   #                                                   #   #   # 
    #   #   # # # # # # # # # # # # # # # # # # # # # # #   #   #   # 
    #   #   #                                           #   #   #   # 
    #   #   #   # # # # # # # # # # # # # # # # # # #   #   #   #   # 
    #   #   #   #                                   #   #   #   #   # 
    #   #   #   #   # # # # # # # # # # # # # # #   #   #   #   #   # 
    #   #   #   #   #                           #   #   #   #   #   # 
    #   #   #   #   #   # # # # # # # # # # #   #   #   #   #   #   # 
    #   #   #   #   #   #                   #   #   #   #   #   #   # 
    #   #   #   #   #   #   # # # # # # #   #   #   #   #   #   #   # 
    #   #   #   #   #   #   #           #   #   #   #   #   #   #   # 
    #   #   #   #   #   #   #   # # #   #   #   #   #   #   #   #   # 
    #   #   #   #   #   #   #   #       #   #   #   #   #   #   #   # 
    #   #   #   #   #   #   #   # # # # #   #   #   #   #   #   #   # 
    #   #   #   #   #   #   #               #   #   #   #   #   #   # 
    #   #   #   #   #   #   # # # # # # # # #   #   #   #   #   #   # 
    #   #   #   #   #   #                       #   #   #   #   #   # 
    #   #   #   #   #   # # # # # # # # # # # # #   #   #   #   #   # 
    #   #   #   #   #                               #   #   #   #   # 
    #   #   #   #   # # # # # # # # # # # # # # # # #   #   #   #   # 
    #   #   #   #                                       #   #   #   # 
    #   #   #   # # # # # # # # # # # # # # # # # # # # #   #   #   # 
    #   #   #                                               #   #   # 
    #   #   # # # # # # # # # # # # # # # # # # # # # # # # #   #   # 
    #   #                                                       #   # 
    #   # # # # # # # # # # # # # # # # # # # # # # # # # # # # #   # 
    #                                                               # 
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
    

    Explanation:

    Let i represent the current row index and j represent the current column index.

    Split the problem into two halves. The bottom half is easier because the left and right halves are symmetrical, so both even and odd rows print their respective pattern on alternating column indices if j < size - i || j > i.

    The top half is a little trickier: the pattern is the same, except the left side lags behind the right side by 1, meaning we need to subtract 1 from the left side: if j < i - 1 || j >= size - i.

    Note that this code could use some cleanup to reduce repetition; I left it verbose so that it's clear how it works. I also took the liberty of adjusting the characters to print because the original is harder on the eyes--this is easily adjusted using the #defines or adding parameters to the function.