-2

I have a 2D vector vector of characters grid[j][i] filled with various letters, I would like to create another 2D vector of characters twice the size of grid[j][i] filled alternately with points '.'(or spaces ' ') and grid[j][i] values.

For example :

      grid                    new 2D vector       

    a b c d                   a . b . c . d        
    e f g h                   . . . . . . .
    i j k l                   e . f . g . h
                              . . . . . . .
                              i . j . k . l

Does anyone have an idea of how to implement this in C++ using vectors ?

Thanks in advance for any help you could provide.

Joshua
  • 40,822
  • 8
  • 72
  • 132
Reblochon Masqué
  • 55
  • 1
  • 1
  • 10

1 Answers1

3

Easy right? If the row or column number is a multiple of 2 (1 based counting) then you want a . or ' ' inserted.

Here is some psuedo code:

for each row
    if (row+1 mod 2)
        add a row of .
    else
        for each column
            if (col+1 mod 2)
               add .
            add grid[row][col]

and you are done!

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • Note that the order of the loops---row, column---is not best practice since C/C++ is row-major. – Richard Dec 21 '16 at 22:13
  • 1
    @Richard Can you elaborate? In a 2D vector in c++ the row/column order can be whatever i choose it to be? – Fantastic Mr Fox Dec 21 '16 at 22:37
  • @Richard According to [this wikipedia entry](https://en.wikipedia.org/wiki/Row-_and_column-major_order) and [this question](http://stackoverflow.com/questions/33722520/why-is-iterating-2d-array-row-major-faster-than-column-major), this is the correct order to iterate for speed purposes. – Fantastic Mr Fox Jan 03 '17 at 20:16
  • My apologies, I had a temporary bout of dyslexia. I meant to come back and correct myself, but got distracted. – Richard Jan 05 '17 at 17:55