Click here to Skip to main content
14,699,089 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Given a number N. Print a spiral that looks like the output below.
Input
The input consist an integer N (1 ≤ N ≤ 101) , that represents the number N. It is guaranteed
that N mod 4 = 1
Output
Output spiral that looks like in sample case.
1st​ ​Sample​ ​Input
1

Output​ ​for​ ​1st​ ​Sample​ ​Input
#


2nd​ ​Sample​ ​Input
5

Output​ ​for​ ​2nd​ ​Sample​ ​Input
#####
....#
###.#
#...#
#####

3rd​ ​Sample​ ​Input
9

Output​ ​for​ ​3rd​ ​Sample​ ​Input
#########
........#
#######.#
#.....#.#
#.###.#.#
#.#...#.#
#.#####.#
#.......#
#########

4th​ ​Sample​ ​Input
11

Output​ ​for​ ​4th​ ​Sample​ ​Input
###########
..........#
#########.#
#.......#.#
#.#####.#.#
#.#...#.#.#
#.#.###.#.#
#.#.....#.#
#.#######.#
#.........#
###########


What I have tried:

I can only get it to 11

#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;

}
Posted
Updated 8-Dec-17 11:32am
v3
Comments
Patrice T 8-Dec-17 9:12am
   
Show your code.
F-ES Sitecore 8-Dec-17 9:27am
   
Start with the top left # and follow it right, down, left, up, right and so on and note the length of each line in # characters and see if you can spot a pattern there.
Rick York 8-Dec-17 10:39am
   
I think Jochen's approach is a good one to pursue. Make a NxN array, also called a matrix, of points and then you can traverse them in a spiral path and mark where the hashes go.

You need some logic for your printing. A function which resolves some problems.

int input = 0;
//Do a check of input
while( (input % 4) != 1 ) {
  input = AskFromUser();
}
int row = input//looks like the values are the same
int colums = input;//looks like the values are the same

for( int y = 0; y < rows; y++ ) {
  for( int x = 0; x < colums; x++ ) {
    bool result = IsPrintingPoint(x,y, input);
    printf( result ? "." : "#");
  }
  printf("\n");
}
The magic is the IsPrintingPoint function.
bool IsPrintingPoint( int x, int y, int rows ) {
  //quick check
  if( (y == 0) || (y == rows -1)) return true;

  //TODO: here comes your logic for deciding what to print

  return false;
}
   
I have formatted the example outputs so that they are shown with a fixed font.

Have a look at them and you might find the algorithm. Think about how you would draw it with a pencil on a graph paper (here with one based x/y indexes):
1,1 - 1,N - N,N - 1,N - 1,3 - N-2,3 - N-2,N-2, ....

An approach might be creating a 2-dimensional array of N x N characters like the graph paper. Initialise the array with dots so that the hash characters can be placed by the "drawing" function.

All you need now is the drawing function. If you extend my above drawing series you should find the algorithm. Note that the 3 in my series can be also written as 1 + 2.
   
v3
First of all, you need to analyze the problem.
999999999
........9
9955555.9
9.....5.9
9.551.5.9
9.5...5.9
9.55555.9
9.......9
999999999

The spiral of 9 includes the spiral of 5, which include the spiral of 1.
One can guess it will continue with 13, 17, 21, 25 ...
You can't use specific handling for every spiral sizes, you have to find another way.
In this case, the easiest is using a 2d array that you will use like a sheet of paper.
Put the pencil at starting point and draw first line, then second and so on.
You only have to handle how second you draw the second loop in reduced square.
Think how it will translate to code.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900