BeginnersBook.in

Programming For Geeks

  • Home
  • All Tutorials
    • Learn C
    • Learn Python
  • Programs
    • Java Programs
    • C++ Programs
    • C Programs
  • Algorithms
    • Sorting
  • MCQ’s
    • MySQL
    • Operating System
  • WordPress
    • Genesis Pro Themes
    • Blogs
  • Tools
    • Direct Download Link Generator
    • HTML Compiler
  • How to
    • Web Examples
  • Write For Us

C++ Program to Print Spiral Matrix

May 15, 2018 By Mr. Robot

In this program, you will learn how to print spiral matrix on the output console. The spiral matrix or circular matrix of order n*n in both clockwise and anti-clockwise directions. see the following example:

spiral matrix

Example: Program to Display Spiral Matrix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <vector>
// for auto_ptr
#include <memory>
// for the ceil and log10 and floor functions
#include <cmath>
#include <iostream>
// for the setw function
#include <iomanip>
 
using namespace std;
 
typedef vector<int> IntRow;
typedef vector<IntRow> IntTable;
 
auto_ptr<IntTable> getSpiralArray(int dimension)
{
    auto_ptr<IntTable> spiralArrayPtr(new IntTable(
            dimension, IntRow(dimension)));
 
    int numConcentricSquares = static_cast< int >( ceil(
            static_cast< double >( dimension ) / 2.0));
 
    int j;
    int sideLen = dimension;
    int currNum = 0;
 
    for (int i = 0; i < numConcentricSquares; i++)
    {
        // do top side
        for (j = 0; j < sideLen; j++)
            (*spiralArrayPtr)[i][i + j] = currNum++;
 
        // do right side
        for (j = 1; j < sideLen; j++)
            (*spiralArrayPtr)[i + j][dimension - 1 - i] = currNum++;
 
        // do bottom side
        for (j = sideLen - 2; j > -1; j--)
            (*spiralArrayPtr)[dimension - 1 - i][i + j] = currNum++;
 
        // do left side
        for (j = sideLen - 2; j > 0; j--)
            (*spiralArrayPtr)[i + j][i] = currNum++;
 
        sideLen -= 2;
    }
 
    return spiralArrayPtr;
}
 
void printSpiralArray(const auto_ptr<IntTable> &spiralArrayPtr)
{
    size_t dimension = spiralArrayPtr->size();
 
    int fieldWidth = static_cast< int >( floor(log10(
            static_cast< double >( dimension * dimension - 1 )))) + 2;
 
    size_t col;
    for (size_t row = 0; row < dimension; row++)
    {
        for (col = 0; col < dimension; col++)
            cout << setw(fieldWidth) << (*spiralArrayPtr)[row][col];
        cout << endl;
    }
}
 
int main()
{
    printSpiralArray(getSpiralArray(5));
 
    return 0;
}
When you run the program, the output will be following:
1
2
3
4
5
0  1  2  3  4
15 16 17 18  5
14 23 24 19  6
13 22 21 20  7
12 11 10  9  8

Filed Under: C++ Examples

Related Posts

  • C++ Program to Calculate Area of Circle by Calling Function
  • C++ Program to Find Fibonacci Numbers using Recursion
  • C++ Program to Print Pascal Triangle

Recent Posts

  • Download Grand Theft Auto: Vice City Audio File | Highly Compressed
  • Grand Theft Auto: Vice City Game Download For PC | Full Setup
  • SQL SHOW DATABASES Statement
  • Direct view storage Tube (DVST) in Computer Graphics
  • CRT Color Monitor in Computer Graphics

C++ Programs

  • More 300+ Examples
  • “Hello World” Program
  • Add Two Numbers
  • Area of Circle
  • Even or Odd Number
  • Find Factorial Number
  • Simple Calculator Program
  • Swap Two Numbers
  • Check Vowel and Consonant
  • Reverse a Number
  • Fibonacci Series
  • Leap Year
  • Toggle Characters
  • Check Super Digit
  • Check Anagram Strings
  • Spy Numbers
  • Check Automorphic Number
  • Check Neon Number
  • Check Pandigital Number
  • Strong Number
  • Inverse of Matrix

More C++ Examples…

  • Star(*) Pattern Programs
  • Wave Pattern Programs
  • Sorting Algorithms

Tutorials

  • C Tutorial

Copyright © 2019 - BeginnersBook.in - All Right Reserved || Sitemap