Thread: Transpose of a 2 dimensional array

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    19

    Transpose of a 2 dimensional array

    Hi--
    I am trying to figure out how to get the coding for the transpose of a 2-D array to properly work. So far I can successfully generate the array and to print it out but whenever I try to add the part where it will transpose the array, the program simply crashes. I believe that I have something wrong with my indexing in the transpose function part (void trans) but am not sure. I'd appreciate any help. Thanks. Here is the code:

    Code:
    #include <iostream>
    #include <ctime>       // For time()  //
    #include <cstdlib>     // For srand() and rand() //
    #include <iomanip>
    #define ROW 5
    #define COL 10
    
    using namespace std;
    using std::setw;
    
    
    void dis(int x[][COL],int,int);
    void trans(int x[][COL],int,int);
    
    int main()
    {
    	int x[ROW][COL];
    	int rnum;
    	int t[ROW];
    	
    	cout << "The values of the original array are:\n\n";
    	srand(time(0));       // Initialize random number generator. 
    	rnum = (rand() % 100) + 1;
    	for(int r=0; r<ROW; r++)       //row	
           { 
            for(int c=0; c<COL; c++)
                x[r][c] = (rand()%100) + 1;
           }
    	
    	dis(x,ROW,COL);
    	trans(x,ROW,COL);
    	cout << "\nThe values of the modified array are:\n\n";
    	dis(x,ROW,COL);
    	system("pause");		
    	return 0;  // indicates successful termination
    } // end main	
    
    
    void dis(int x[5][10],int r,int c)
    {    
        for(int r=0; r<ROW; r++)       //row	
    	   { 
            for(int c=0; c<COL; c++)   //column
    	       { 
    		      cout << setw(5) << x[r][c] << ' ';   //display Array
               }
    		cout << "\n" << endl;
            } 
    }
    
    void trans(int mat[][10],int r ,int c)
    {
       int temp;
       int x[r][c];
    
    for (int r = 0; r < ROW; r++)
       for (int c = 0; c < COL; c++)
          { temp = x[r][c];
            x[r][c] = x[c][r];
            x[c][r] = temp;
          }
    }

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    It's because the row and column have different dimensions, so when you swap matrix[r][c] with matrix[c][r], you're trying to put 10 elements where you only have room for 5.

    edit:
    I suppose I should tell you how to fix it too.

    I would use a 1-dimensional array with ROW*COL elements. Then you can subscript it with [r*COL+c]
    Last edited by NeonBlack; 04-29-2010 at 06:51 PM.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 2 dimensional array to node structure
    By pawikan in forum C++ Programming
    Replies: 10
    Last Post: 12-02-2009, 04:19 AM
  2. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-09-2008, 11:53 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Replies: 5
    Last Post: 11-20-2001, 12:48 PM