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

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    69

    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. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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;
         }
    }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  3. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM