C++ Fill a 2D vector with alternate values

510 views Asked by At

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.

1

There are 1 answers

4
Fantastic Mr Fox On

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!