Spiral Structure

Write a program for generating a spiral structure of N number.

For example: If N= 4 then the structure will be as follow-
1 2
4 3



If N=5, then the structure will be as follow-
1 2
5 4 3



If N=9, then the structure will be as follow-


7 8 9
6 1 2
5 4 3



If N=15, then the structure will be as follow-
7 8 9 10
6 1 2 11
5 4 3 12
15 14 13



If N=18, then the structure will be as follow-
7 8 9 10
6 1 2 11
18 5 4 3 12
17 16 15 14 13

Questions by ako12345

Showing Answers 1 - 1 of 1 Answers

Gargona

  • Feb 13th, 2010
 

/*
 * File:   Spiral.cpp
 */

#include <cmath>
#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

class Spiral {
    int number; // number elements
    int asize;  // matrix size
    int findex; // indexes of the element equal to '1'
    vector<vector<int> > matrix;
   public:
    Spiral(size_t num = 1) : number(num) {
        asize = sqrtf(number); asize += (number - powf(asize,2)) > 0 ? 1 : 0;
        findex = asize/2 + asize%2 -1;
        vector<int> clmn;
        matrix.assign(asize, clmn);
        for (int i=0; i< asize; ++i)
            matrix[i].assign(asize, 0);
        matrix[findex][findex] = 1;
    }
    void FormSpiral() {
        enum {
            east,
            down,
            west,
            up
        } direction;
        int row, col, val;
        bool start = true;

        row = col = findex;
        direction = east;
        for (val=2; val <= number; ++val) {
            switch (direction) {
                case east:
                    if (matrix[row+1][col] == 0 && start == false) {
                        direction = down;
                        matrix[++row][col] = val;
                    }
                    else {
                        start = false;
                        matrix[row][++col] = val;
                    }
                    break;
                case down:
                    if (matrix[row][col-1] == 0) {
                        direction = west;
                        matrix[row][--col] = val;
                    }
                    else {
                        matrix[++row][col] = val;
                    }
                    break;
                case west:
                    if (matrix[row-1][col] == 0) {
                        direction = up;
                        matrix[--row][col] = val;
                    }
                    else {
                        matrix[row][--col] = val;
                    }
                    break;
                case up:
                    if (matrix[row][col+1] == 0) {
                        direction = east;
                        matrix[row][++col] = val;
                    }
                    else {
                        matrix[--row][col] = val;
                    }
                    break;
            }
        }
    }
    void PrintSpiral() {
        for (int i=0; i<asize; ++i) {
            for (int j=0; j<asize; ++j){
                if (matrix[i][j] != 0) {
                   cout << matrix[i][j] << " ";
                }
            }
            cout << endl;
        }
    }
private:
    Spiral(const Spiral& sp);

};

// ArrPointer::ArrPointer(int r, int c) : row(r), column(c) {}

const int x = 46;

void usage(char * str) {
     cout << "usage: " << str << " number (number > 0)" << endl;
}

int main(int argc, char **argv) {
    if (argc < 2) {
        usage(argv[0]);
        return 1;
    }

    int number = atoi(argv[1]);

    if (!number) {
        usage(argv[0]);
        return 1;
    }

    Spiral sp(number);
    sp.FormSpiral();
    sp.PrintSpiral();
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions