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

Discussion in 'Programming' started by djzmo, Jan 29, 2009.

  1. #1
    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++;
            }
        }
    Code (markup):
    and it produces... (illustrated below)

    Now my problem is making it spiral..

    [​IMG]

    any ideas?

    (sorry i made the illust. using mspaint :p)
     
    djzmo, Jan 29, 2009 IP
  2. djzmo

    djzmo Active Member

    Messages:
    165
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #2
    anyone? :(
     
    djzmo, Jan 30, 2009 IP
  3. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    819
    Best Answers:
    7
    Trophy Points:
    320
    #3
    Looks like a very simple programming problem to me. Unfortunately I don't know anything about the C languages.

    In FoxPro I would just fill in the array in a spiral fashion while incrementing your letters by one each time. I suspect that if I played around with it in FP, I could probably do it in less than 10 lines of code.

    Like fill the first row, then fill the last column, then fill the last row backwards, then fill the first row less one backwards, then fill the 2nd row less one, and so on until done.
     
    mmerlinn, Jan 30, 2009 IP