# Thread: Copying from one 2d array to another....with a twist

1. ## Copying from one 2d array to another....with a twist

Hello,

I'm trying to copy one 2d array in another. However, in the one that I'm copying into, I want to have spaces in between each letter and a space in between each row. Here is what I have so far, but it only prints out the first letter of each row:

Code:
```#include <iostream>
#include <map>
#include <cmath>

using namespace std;

int main() {
// potential letters
char letters[] = {'a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w','x','y','z'};

char city[26][26] = {0};     // 2d array to hold input
string line; // for reading in strings
int row = 0; // keep track of number of rows

map<char,int> values;  // map letters to number values

// initialize map
for (int i = 0; i < 26; i++)
values[letters[i]] = i;

// put input into 2d array
while ( getline(cin,line) ) {
for (int i = 0; i < line.length(); i++)
city[row][i] = line[i];
row++;
}

// 2d array to hold map of city
int row2 = (2*row)-1;
char cityMap[5][12] = {0};

for (int i = 0,l=0; l < row; i+=2,l++) {
for (int j = 0,k=0; j < 12; j+=2,k++) {
if (city[l][k] == 0) break;
cityMap[i][j] = city[l][k];
}
}

for (int i = 0; i < 5; i++) {
for (int j = 0; j < 12; j++) {
if (cityMap[i][j] == 0) break;
cout << cityMap[i][j];
}
cout << endl;
}

}```
I know that loop to fill up the cityMap array is ugly and that's where I'm doing something wrong, I think.

Here's some input and what the output should be:

acabab
bcdefg
dcbabb
a c a b a b

b c d e f g

d c b a b b
Currently, I am getting the following for output:
a

b

d
If someone could show me how to alter my code to make that output happen, that would be greatly appreciated.

2. that is way too much...break it down into functions...

and do something like this

Code:
```for(int i = 0; i < x; i++)
{
for(int j = 0; j < y; i++)
{
char *c = something[i][j];
if(c == somethingelse)
//something
foo[i][j] = *c;
}
}```

3. Code:
```This:

char cityMap[5][12] = {0};

initializes all elements of cityMap to 0.  This:

for (int i = 0,l=0; l < row; i+=2,l++) {
for (int j = 0,k=0; j < 12; j+=2,k++) {
if (city[l][k] == 0) break;
cityMap[i][j] = city[l][k];

puts a char from city into every other postion of cityMap because j increments by 2 each time through the inner loop, meaning:

cityMap[0][0] == 'a';
cityMap[0][1] == 0;
cityMap[1][0] == 'b';
cityMap[1][1] == 0;
etc.

That means that this:

for (int i = 0; i < 5; i++) {
for (int j = 0; j < 12; j++) {
if (cityMap[i][j] == 0) break;
cout << cityMap[i][j];
}
cout << endl;
}

will break from the inner loop, start a new line and increment i each time j == 1 only allowing you to only display:

a
b
d```