1

I´m trying to make a spiral pattern (just starting with C programming). I try to do it manually for the start from n=1 to n=15 but when the n=12 or more it starts to get messy. Maybe someone could give me a hint. Yes I know this is not a website for beginners/students/noobs but I couldn´t find help elsewhere.

For now I have this:

#include<stdio.h>

int main(){
    int n;
    scanf("%d",&n);
    fflush(stdin);

    for(int i = 0;i<n;i++){
        for(int j = 0;j<n;j++){
            if(i==0||i==n-1||j==n-1)
                printf("#");
            else if(j==0&&i>1&&i<n-1)
                printf("#");
            else if(i==2&&j<n-2)
                printf("#");
            else if(j==n-3&&i>2&&i<n-2)
                printf("#");
            else if(i==n-3&&j<n-3&&j>1)
                printf("#");
            else if(j==2&&i>3&&i<n-2)
                printf("#");
            else if(i==4&&j>2&&j<n-4)
                printf("#");
            else if(j==6&&i>4&&i<n-4)
                printf("#");
            else if(i==6&&j>3&&j<n-5)
                printf("#");
            else printf(".");

        }
        printf("\n");
    }
    getchar();
    return 0;

}

Those are the examples of the outputs:

For n=7

#######
......#
#####.#
#...#.#
#.###.#
#.....#
#######

For n=9

#########
........#
#######.#
#.....#.#
#.###.#.#
#.#...#.#
#.#####.#
#.......#
#########

But now when n=13

#############
............#
###########.#
#.........#.#
#.#######.#.#
#.#...#...#.#
#.#.####..#.#
#.#...#...#.#
#.#...#...#.#
#.#.......#.#
#.#########.#
#...........#
#############

It becomes a mess.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    You've hardcoded tests up to i and j equal to 6, which is half of 12. Without diving into the logic, that's probably got to do something with the fact that things get bad above 12. –  Dec 24 '17 at 22:42
  • Looking at all those `if`s I cannot but think "there must be a pattern in there"... – Jongware Dec 24 '17 at 22:48
  • 3
    Sit down and use pen and paper and come up with an algorithm...there is a pattern to this problem and the code size would be at least half of what you have right now. Start by asking the user only for a dimension and make everything else dynamic (maybe create a buffer array or matrix on the heap?) – Dumbo Dec 24 '17 at 22:55
  • Choose some large size, like 30 or so, and draw the spiral by hand. Look at the **even** rows and **odd** rows above the centre, and even and odd rows below the centre, separately. Figure out the pattern for each. Also do this for even and odd spiral sizes separately. – n. m. could be an AI Dec 24 '17 at 22:58
  • @SaeidYazdani has the correct approach. I would add that you should consider a matrix that is N by N dots. How would you walk through such a field leaving '#' symbols behind as you go? – jwdonahue Dec 24 '17 at 23:00
  • Note the limitations on [Using `fflush(stdin)`](https://stackoverflow.com/questions/2979209/). – Jonathan Leffler Dec 24 '17 at 23:45
  • @SaeidYazdani "create a buffer array or matrix" where's the challenge in that? – n. m. could be an AI Dec 24 '17 at 23:49
  • Maybe you need to (a) show that 11 works, and (b) draw what you think is required for 13, and (c) work out a formulae (function) that, given an pair of coordinates (row, column, or x, y) and the spiral size produces the correct character. You can then work on the formula in that function. It's going to be modestly interesting — you would need to check that it's correct for size 15, 17, etc too. What should happen when even numbers are input? When does it break-down then? – Jonathan Leffler Dec 24 '17 at 23:49
  • @n.m. A matrix can help ease the problem as it can be addressed in multiple ways, e.g. By each row or column or a set of (x, y) coordinates. – Dumbo Dec 25 '17 at 00:38
  • You start at 0,0 and walk to 0,N, turn right and ... etc. The point is, once you think that through completely and look at the menu of string manipulation functions in string.h, you'll find an extremely simple O(2N) solution to the problem. – jwdonahue Dec 25 '17 at 03:52
  • @SaeidYazdani yes this problem is easy if you have all the memory you want. – n. m. could be an AI Dec 25 '17 at 08:26
  • Possible duplicate of [How to draw a spiral of # with only for/while loops and if/else statements (without arrays) using C](https://stackoverflow.com/questions/51691446/how-to-draw-a-spiral-of-with-only-for-while-loops-and-if-else-statements-with) – ggorlen Aug 06 '18 at 01:34

0 Answers0