[urgent] Filling a table (multidimensional array) with spiral datas..

Jan 29, 2009 at 2:33pm
Hello

I need to create a table (using multidimensional array/vector) that contains datas in a spiral form. if you dont know how spiral looks like, http://images.google.com/images?q=spiral

Here's my work so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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]http://img210.imageshack.us/img210/3671/tospiralub1.jpg[/img]

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 :)

(sorry i made the illust. using mspaint :p)
Jan 29, 2009 at 3:36pm
Have you tried making a much larger one, say 10x8 and working thru that by hand to get an algorithm?
Jan 29, 2009 at 3:40pm
Not yet.

But I think it wont be flexible.

Because in the real program, the 'rows' and 'columns' variables are inputted by user, so it will be vary.
Jan 29, 2009 at 3:54pm
Yes, but if you work with a couple of large grids by hand, you'll have gone thru it enough times to "know" what to do. You just implement that algorithm. You really should try it.
Feb 2, 2009 at 7:01pm
as kbw said, you need to run it by hand, get the values(rows,columns) that change as you fill the spiral, find out how they change(key point)
i did it with a 2x3 grid, grid size dont matter, let me give you a tip,

you need 4 variables to store what has been filled. (startrow,startcolumn,endrow,endcolumn)
you need 4 loops(for)

one going from left(1st available column-startcolumn-) to right(last available column-endcolumn-) on first available row.

one going from first row til last on the last available column.

one going from right(last available column) to left(1st available column) on last available row.

one going from last available row til 1st available row on the first available column.

Last edited on Feb 2, 2009 at 7:03pm
Topic archived. No new replies allowed.