Q:

C++ program to print a spiral matrix

0

C++ program to print a spiral matrix

A spiral matrix is a matrix consist of natural numbers up to n^2. Starting from one the numbers start increasing around the corner matrix then goes inside up to the middle element making the shape of the spiral or jalebi

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

For example: A spiral matrix for n=5

1   2   3   4   5
16  17  18  19  6
15  24  25  20  7 
14  23  22  21  8
13  12  11  10  9

Algorithm:

In this algorithm the size of the array decreases by 2 for every iteration.

We starts with filling the first row then last column then last row after that the first column that means we will fill array around the corner and then for next iteration we will take the array inside the last array and fill it around the corner and so on until we reach the centre.

For first iteration array will be:

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

Here the empty or un-accessed elements are denoted by "0".

For next iteration array will be:

_   _   _   _   _
_   17  18  19  _
_   24  0   20  _
_   23  22  21  _
_   _   _   _   _

Here "_" represents the already accessed elements and “0”represents the un-accessed elements.

The size of the array goes on decreasing until you reach the centre most array element.

Consider the program:

#include<iostream>
using namespace std;

int main()
{
	int n;
	cout<<"Enter the size of the array :";
	cin>>n;                         /*n Size of the array*/        
	int A[n][n];
	int len=n,k=1,p=0,i;            /*k is to assign the values to the array from 1...n*n */
									/*len is used to update(decrease) array size so that values cans be assign to them */
	while(k<=n*n)                      
	{
		for(i=p;i<len;i++)         /*Loop to access the first row of the array*/
		{
			A[p][i]=k++;
		}
		for(i=p+1;i<len;i++)      /*Loop tp access the last column of the array*/
		{
			A[i][len-1]=k++;
		}
		for(i=len-2;i>=p;i--)     /*Loop to access the last row of the array*/ 
		{
			A[len-1][i]=k++;
		}
		for(i=len-2;i>p;i--)      /*Loop to access the first column of the array*/
		{
			A[i][p]=k++;
		}
		p++,len=len-1;
		
	}
	if(!n%2)                      /*This block will run only if n is even*/
	{
		A[(n+1)/2][(n+1)/2]=n*n; /*It will assign the last value to the centremost element*/
	}
	for(i=0;i<n;i++)             /*This loop will print the array in matrix format*/
	{
		for(int j=0;j<n;j++)
		{
			cout<<A[i][j]<<"\t";
		}
		cout<<endl;
	}
	return 0;
}

Output

First Run:
Enter the size of the array :7                    
1       2       3       4       5       6       7 
24      25      26      27      28      29      8 
23      40      41      42      43      30      9 
22      39      48      49      44      31      10
21      38      47      46      45      32      11
20      37      36      35      34      33      12
19      18      17      16      15      14      13

Second Run:
Enter the size of the array :10                                           
1       2       3       4       5       6       7       8       9       10
36      37      38      39      40      41      42      43      44      11
35      64      65      66      67      68      69      70      45      12
34      63      84      85      86      87      88      71      46      13
33      62      83      96      97      98      89      72      47      14
32      61      82      95      100     99      90      73      48      15
31      60      81      94      93      92      91      74      49      16
30      59      80      79      78      77      76      75      50      17
29      58      57      56      55      54      53      52      51      18
28      27      26      25      24      23      22      21      20      19 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Most popular and Searched C++ solved programs with Explanation and Output

Similar questions


need a help?


find thousands of online teachers now
C++ program to check whether a string2 can be form... >>
<< C++ program to find the frequency of a character i...