Home > Article > Backend Development > C++ program to print spiral pattern of numbers
Displaying numbers in different formats is one of the problems of learning basic coding.
Different coding concepts like conditional statements and loop statements. have In different programs we use special characters like asterisks to print triangles or square. In this article, we will print numbers in a spiral, just like the squares in C.We take the number of rows n as input, and then start from the upper left corner Move to the right, then down, then left, then up, then right again, and so on etc.
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
To solve this problem, we will use a two-dimensional matrix of size n x n, in this case, we take n = 7. Then fill the matrix in a spiral starting from the upper left corner. final printout the entire matrix. Here we are printing from 1 to 7 on the first line and then the process is changing it's direction, move toward the bottom to 13, then to the left to 19, and finally Go up until 24, then right, and so on. Let's look at a better algorithm understand.
#include <iostream> using namespace std; void solve( int s ){ int mat[ s ][ s ] = {0}; int i, j, m, n, p, q, num; num = 1; // start count from 1 i = 0; j = 0; m = 0; // row index lower limit n = s - 1; // row index upper limit p = 0; // column index lower limit q = s - 1; // column index upper limit while ( num <= s * s ) { // place numbers horizontally left to right for ( j = p; j <= q; j++ ) { mat[ m ][ j ] = num; num = num + 1; } m = m + 1; // fill vertically from top to bottom for ( i = m; i <= n; i++ ) { mat[ i ][ q ] = num; num = num + 1; } q = q - 1; // fill horizontally from right to left for ( j = q; j >= p; j-- ) { mat[ n ][ j ] = num; num = num + 1; } n = n - 1; // fill vertically from bottom to top for ( i = n; i >= m; i-- ) { mat[ i ][ p ] = num; num++; } p = p + 1; } // display the mat for ( i = 0; i < s; i++ ) { for ( j = 0; j < s; j++ ) { printf("%d\t", mat[i][j]); } printf("\n"); } } int main(){ int n = 5; cout << "Spiral numbers for " << n << " lines." << endl; solve( n ); }
Spiral numbers for 5 lines. 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
Spiral numbers for 12 lines. 1 2 3 4 5 6 7 8 9 10 11 12 44 45 46 47 48 49 50 51 52 53 54 13 43 80 81 82 83 84 85 86 87 88 55 14 42 79 108 109 110 111 112 113 114 89 56 15 41 78 107 128 129 130 131 132 115 90 57 16 40 77 106 127 140 141 142 133 116 91 58 17 39 76 105 126 139 144 143 134 117 92 59 18 38 75 104 125 138 137 136 135 118 93 60 19 37 74 103 124 123 122 121 120 119 94 61 20 36 73 102 101 100 99 98 97 96 95 62 21 35 72 71 70 69 68 67 66 65 64 63 22 34 33 32 31 30 29 28 27 26 25 24 23
Displaying number patterns is a fairly common problem when learning programming language. In this article we learned how to display numbers in the square in which the element is located Print in a spiral form in C, starting from the top left corner and moving downwards At the end of column n, we move down, then at the end of row n, we move left, then After reaching the first row, move up until row 2nd, then repeat this process over and over again until... Complete the entire square. Unlike other numeric pattern problems, it requires a 2D array Solve this problem effectively.
The above is the detailed content of C++ program to print spiral pattern of numbers. For more information, please follow other related articles on the PHP Chinese website!