swapping elements in a 2D array

This is a discussion on swapping elements in a 2D array within the C++ Programming forums, part of the General Programming Boards category; hey guys, this is another part of my program which I'm having a hard time with. For those who followed ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    swapping elements in a 2D array

    hey guys, this is another part of my program which I'm having a hard time with. For those who followed my previous post about displaying a board, now I'm trying to let the user swap the letters within the array. Something like this:
    Code:
      1 2 3 4 5
      - - - - - 
    A|A O E A E
    B|H E Y F U
    C|D T A A O
    D|L E S B I
    E|O W O S N
    
    Enter the coordinates of the locations to swap: A1 B5
    
    
      1 2 3 4 5
      - - - - - 
    A|U O E A E
    B|H E Y F A
    C|D T A A O
    D|L E S B I
    E|O W O S N
    I'm not exactly how to do it with strings. Because comparing each inputed string to every one set up for the board positions would be extremely long. The approach I took will be probably long as well, but for now it is not working. In the code below, once the elements have been swapped and I want to display the board again, I get all Z's:
    Code:
    void displayTheBoard(float totalPercentArray[ ], char boardArray[][SIZE])
    {
    	int i; 
    	int j;
    	int move = 1;
    
    	cout << "   1 2 3 4 5\n"
    		 << "   - - - - -\n";
    	for( i = 0; i < SIZE; i++)
    	{
    		cout << (char)(i + 'A') << "| ";
    		for ( j = 0; j < SIZE; j++ )
    		{
    			if( move == 1){
    			boardArray[i][j] = getCharacter(totalPercentArray);
    			}
    			cout << boardArray[i][j] << " ";
    		}
    		cout << endl;		
    	}
    	move++;
    	swapLetters(boardArray);
    }
    
    //==============================================
    void swapLetters(char boardArray[][SIZE])
    {
    	//declaring variables
    	char letterFrom;
    	char letterTo;
    	int numberFrom;
    	int numberTo;
    		
    	cout << "Enter the coordinates of the locations to swap: ";
    	cin >> letterFrom >> numberFrom >> letterTo >> numberTo;
    
    	letterFrom = toupper(letterFrom);
    	letterTo = toupper(letterTo);
    
    	comparePositions(letterFrom, numberFrom, letterTo, numberTo, boardArray);
    }
    
    //=================================
    void comparePositions(char row, int column, char row2, int column2, char boardArray[][SIZE])
    {
    	//declaring varaibles;
    	char temp1;
    	char temp2;
    
    	//declaring arrays
    	float totalPercentArray[LIMIT];
    	
    	if( row == 'A' && column == 1)
    	{
    		if( row2 == 'A' && column2 == 2)
    		{
    			cout << "\n\nmaybe it will work\n\n";
    			temp1 = boardArray[0][0];
    			temp2 = boardArray[0][1];
    			boardArray[0][1] = temp1;
    			boardArray[0][0] = temp2;
    			cout << "first " << boardArray[0][1] << " second " << boardArray[0][0] << endl;
    		}
    	}
    	
    	displayTheBoard(totalPercentArray, boardArray);
    }
    any suggestions? maybe a totaly diferent aproach?
    I'm also attaching the full code for those who want to compile it.

    thanks,

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    forgot to attach the code:

    thanks

    axon
    Attached Files Attached Files
    • File Type: cpp d.cpp (5.8 KB, 126 views)

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    also, here is darwin.txt for completeness

    axon
    Attached Files Attached Files

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,691
    Try to convert the user input into actual row,column indicies.
    For example, "B3" should be converted into 1,2. (Hint: subtract 'A' from the row and 1 from the column).

    Once you have two sets of indicies, you can swap the contents of the matrix similiar to your code which swapped out 0,0 and 0,1.

    gg

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    I'm not esactly sure what you mean with the indicies. But the swapping part that I have doesn't give me the desired result, it just displays a board of 'Z's.

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's an example swapper for you to digest.
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    void swap (char &i1, char &i2)
    {
      char temp = i1;
      i1 = i2;
      i2 = temp;
    }
    
    int main()
    {
      char a[5][5] = {0};
      
      a[0][0] = 'A';
      a[3][3] = 'B';
      cout <<"0,0 = " <<a[0][0] <<endl; 
      cout <<"3,3 = " <<a[3][3] <<endl;
      
      swap (a[0][0], a[3][3]);
      
      cout <<"After swap" <<endl;
      cout <<"0,0 = " <<a[0][0] <<endl; 
      cout <<"3,3 = " <<a[3][3] <<endl;
    }
    
    /* 
     0,0 = A
     3,3 = B
     After swap
     0,0 = B
     3,3 = A
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Thanks again Hammer, but what I can't get to work is displaying these swapped values on my board, without changing the other letters.

    I was thinking of maybe storing each row into a single dimension array? I'm really confused at this point...

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    void swap (char &i1, char &i2)
    {
      char temp = i1;
      i1 = i2;
      i2 = temp;
    }
    
    int main()
    {
      char a[5][5] = {'*'};
      char rows[5] = {'A', 'B', 'C', 'D', 'E'};
      
      int i, j;
    
      a[0][0] = 'A';
      a[3][3] = 'B';
      
      cout << "Before Swap" << endl << endl;
    
      cout << "  1 2 3 4 5" << endl;
    
      for(i = 0; i < 5; ++i)
      {
         cout << rows[i] << ' ';
         for(j = 0; j < 5; ++j)
         {
             cout << a[i][j];
         }
         cout << endl;
       }
      
      
      swap (a[0][0], a[3][3]);
      
      cout <<"After swap" <<endl << endl;
    
      cout << "  1 2 3 4 5" << endl;
    
      for(i = 0; i < 5; ++i)
      {
         cout << rows[i] << ' ';
         for(j = 0; j < 5; ++j)
         {
             cout << a[i][j];
         }
         cout << endl;
       }
      
    }
    the only two values that should change are a[0][0] and a[3][3].

  9. #9
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    thanks very much guys, yesterday night I finally figured it out (and on my own too!) I way to do it. Works sort of like your examples, but still a little different...I'll pos it as soon as I'm completely finished,

    axon
    Last edited by axon; 03-10-2003 at 02:17 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  10. #10
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    here is the solution, it is lengthy and that is because the coordinates inputed are given as a character and a integer. I'm proud of my self, because yesterday the actuall swapping the letter function was 675 lines!

    I had something like this, for each of the 25 positions, 25 options:
    Code:
    if ( from == 1 )
    	{
    		temp = boardArray[0][0];
    		if ( to == 2 ) { boardArray[0][0] = boardArray[0][1]; boardArray[0][1] = temp;}
    		if ( to == 3 ) { boardArray[0][0] = boardArray[0][2]; boardArray[0][2] = temp;}
        .....and so on for a lot of lines
    I noticed that all these followed a certain sequence which I knew could've been written easier and nicer.
    And somehow I figured out a way to split it in two function totalling 86 lines!!!!

    this stuff is really starting to kick in to my mind. I was on the train to school when it came to me! sorry, I'm just excited about it....

    anyways...here it is

    Code:
    void getInput(char boardArray[][SIZE])
    {
    	//declaring strings
    	char strFrom[2];	//holds swap from coordinate
    	char strTo[2];		//holds swap to coordinate
    
    	//declaring variables
    	int from = 0;
    	int to = 0;
    		
    	cout << "Enter the coordinates of the locations to swap: ";
    	cin >> strFrom >> strTo;
    
    	strFrom[0] = toupper(strFrom[0]);
    	strTo[0] = toupper(strTo[0]);
    
    	from = matchPosition(strFrom); //function returning value 1-25 for each
    								   //position on the board
    	to = matchPosition(strTo);	   //function returning value 1-25 for each
    								   //position on the board
    
    	swapPosition(from, to, boardArray);
    }
    
    //=================================
    int matchPosition(char pos[])
    {
    	
    	if( (pos[0] == 'A') && (pos[1] == '1')) { return 1;}
    	else if( (pos[0] == 'A') && (pos[1] == '2')) { return 2; }
    	else if( (pos[0] == 'A') && (pos[1] == '3')) { return 3; }
    	else if( (pos[0] == 'A') && (pos[1] == '4')) { return 4; }
    	else if( (pos[0] == 'A') && (pos[1] == '5')) { return 5; }
    	else if( (pos[0] == 'B') && (pos[1] == '1')) { return 6; }
    	else if( (pos[0] == 'B') && (pos[1] == '2')) { return 7; }
    	else if( (pos[0] == 'B') && (pos[1] == '3')) { return 8; }
    	else if( (pos[0] == 'B') && (pos[1] == '4')) { return 9; }
    	else if( (pos[0] == 'B') && (pos[1] == '5')) { return 10; }
    	else if( (pos[0] == 'C') && (pos[1] == '1')) { return 11; }
    	else if( (pos[0] == 'C') && (pos[1] == '2')) { return 12; }
    	else if( (pos[0] == 'C') && (pos[1] == '3')) { return 13; }
    	else if( (pos[0] == 'C') && (pos[1] == '4')) { return 14; }
    	else if( (pos[0] == 'C') && (pos[1] == '5')) { return 15; }
    	else if( (pos[0] == 'D') && (pos[1] == '1')) { return 16; }
    	else if( (pos[0] == 'D') && (pos[1] == '2')) { return 17; }
    	else if( (pos[0] == 'D') && (pos[1] == '3')) { return 18; }
    	else if( (pos[0] == 'D') && (pos[1] == '4')) { return 19; }
    	else if( (pos[0] == 'D') && (pos[1] == '5')) { return 20; }
    	else if( (pos[0] == 'E') && (pos[1] == '1')) { return 21; }
    	else if( (pos[0] == 'E') && (pos[1] == '2')) { return 22; }
    	else if( (pos[0] == 'E') && (pos[1] == '3')) { return 23; }
    	else if( (pos[0] == 'E') && (pos[1] == '4')) { return 24; }
    	else if( (pos[0] == 'E') && (pos[1] == '5')) { return 25; }
    	else { cout << "\n\nfatal error in compareAndSwap\n\n"; }
    
    }
    
    //==================================================
    void swapPosition(int from, int to, char boardArray[][SIZE])
    {
    	char temp;
    	int i;
    
    	for ( i = 1; i <= 25; i++ )
    	{
    		if(from == i)
    		{
    			if( i >= 1 && i <=5 )
    			{
    				temp = boardArray[0][i-1];
    				matchTo(to, boardArray, temp);
    				boardArray[0][i-1] = temp;
    				break;
    			}
    			else if( i >= 6 && i <=10 )
    			{
    				temp = boardArray[1][i-6];
    				matchTo(to, boardArray, temp);
    				boardArray[1][i-6] = temp;
    				break;
    			}
    			else if( i >= 11 && i <=15 )
    			{
    				temp = boardArray[2][i-11];
    				matchTo(to, boardArray, temp);
    				boardArray[2][i-11] = temp;
    				break;
    			}
    			else if( i >= 16 && i <=20 )
    			{
    				temp = boardArray[3][i-16];
    				matchTo(to, boardArray, temp);
    				boardArray[3][i-16] = temp;
    				break;
    			}
    			else if( i >= 21 && i <=25 )
    			{
    				temp = boardArray[4][i-21];
    				matchTo(to, boardArray, temp);
    				boardArray[4][i-21] = temp;
    				break;
    			}
    		}
    	}
    	displayTheBoard2(boardArray);
    }
    
    //=====================================================
    void matchTo(char to, char boardArray[][SIZE], char& temp)
    {
    	char temp2;
    	int i;
    
    	for ( i = 1; i <= 25; i++ )
    	{
    		if(to == i)
    		{
    			if( i >= 1 && i <=5 )
    			{
    				temp2 = boardArray[0][i-1];
    				boardArray[0][i-1] = temp;
    				temp = temp2;
    				break;
    			}
    			else if( i >= 6 && i <=10 )
    			{
    				temp2 = boardArray[1][i-6];
    				boardArray[1][i-6] = temp;
    				temp = temp2;
    				break;
    			}
    			else if( i >= 11 && i <=15 )
    			{
    				temp2 = boardArray[2][i-11];
    				boardArray[2][i-11] = temp;
    				temp = temp2;
    				break;
    			}
    			else if( i >= 16 && i <=20 )
    			{
    				temp2 = boardArray[3][i-16];
    				boardArray[3][i-16] = temp;
    				temp = temp2;
    				break;
    			}
    			else if( i >= 21 && i <=25 )
    			{
    				temp2 = boardArray[4][i-21];
    				boardArray[4][i-21] = temp;
    				temp = temp2;
    				break;
    			}
    		}
    	}
    }
    what do you guys think?

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 06:37 AM
  2. 2D array becoming "deallocaded"
    By Aaron M in forum C Programming
    Replies: 2
    Last Post: 09-23-2006, 08:53 AM
  3. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 01:39 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21