Hello

I need to create a table (using multidimensional array/vector) that contains datas in a spiral form.

Here's my work so far:

int current_char = 0, rows = 3, columns = 4;
string input = "abcdefghijkl";
vector< vector<string> > str_table(rows, vector<string>(columns));

    for(int i = 0; i < (int)input.length(); i++)
    {
        splitted_input.push_back(input.substr(i, 1));
    }
    for(int x = 0; x < rows; x++)
    {
        for(int y = 0; y < columns; y++)
        {
            str_table[x][y] = splitted_input.at(current_char);
            current_char++;
        }
    }

and it produces... (illustrated below)

Now my problem is making it spiral..

[img][/img]

any ideas?

(sorry i made the illust. using mspaint :p)

Dani AI

Generated

— your current loops fill the matrix row‑by‑row. is on the right track: do top row, right column, bottom row, left column, then repeat for the inner rectangle. The usual, simple and robust approach is iterative: maintain four boundaries (top, bottom, left, right) and move those inward after each side is filled.

Key points to avoid common bugs:

  • Stop when top > bottom or left > right (important for non‑square matrices).
  • If input length != rows*cols, either truncate or pad before filling to avoid index errors.
  • For debugging, first fill the matrix with sequential numbers to verify traversal order before using characters or strings.

C++ example (iterative boundary method):

#include <vector>
#include <string>

std::vector<std::vector<char>> spiral_fill(const std::string &input, int rows, int cols) {
    std::string s = input;
    if ((int)s.size() < rows*cols) s.append(rows*cols - s.size(), ' ');
    std::vector<std::vector<char>> a(rows, std::vector<char>(cols, ' '));
    int top = 0, bottom = rows - 1, left = 0, right = cols - 1;
    size_t idx = 0;
    while (top <= bottom && left <= right) {
        for (int j = left; j <= right; ++j) a[top][j] = s[idx++];
        ++top;
        for (int i = top; i <= bottom; ++i) a[i][right] = s[idx++];
        --right;
        if (top <= bottom) for (int j = right; j >= left; --j) a[bottom][j] = s[idx++];
        --bottom;
        if (left <= right) for (int i = bottom; i >= top; --i) a[i][left] = s[idx++];
        ++left;
    }
    return a;
}

Python equivalent:

def spiral_fill(s, rows, cols):
    s = s[:rows*cols].ljust(rows*cols)
    res = [[' ']*cols for _ in range(rows)]
    top, bottom, left, right = 0, rows-1, 0, cols-1
    it = iter(s)
    while top <= bottom and left <= right:
        for j in range(left, right+1): res[top][j] = next(it)
        top += 1
        for i in range(top, bottom+1): res[i][right] = next(it)
        right -= 1
        if top <= bottom:
            for j in range(right, left-1, -1): res[bottom][j] = next(it)
            bottom -= 1
        if left <= right:
            for i in range(bottom, top-1, -1): res[i][left] = next(it)
            left += 1
    return res

If results look wrong, print the matrix of indices first (0,1,2,...) to confirm traversal order before mapping your characters. This approach handles rectangular grids cleanly and is easier to reason about than a recursive version.

Recommended Answers

All 3 Replies

Well What exactly does Spiral mean?

Secondly could you just illustrate what is the desired output your code like you did with the mspaint example.

I mean, it looks like spiral..if you dont know how spiral looks like, http://images.google.com/images?q=spiral

now look at my illustration back
I wanna read it from left to right, from top to bottom..
so, normally it will be read as

abcdefghijkl

but after I reformed it to spiral,

abcdjkleihgf..


any ideas? help would be greatly appreciated :)

I guess you should first fill in the numbers in the 1st row then the last column and then the last row then the first colomn. After that make it recrusive for the columns and rows inside and you can have that.


abcd
k l e
j i h g f

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.