//File:   Demarco_3_2
//Author: Jesse Demarco
//Date:   Mar. 7, 2007
//
//Description: This program prompts the user to input two integers for the
//             rows and columns of a matrix. Programs uses a dynamic array and
//             fills the matrix with random numbers. A structure is used to
//             completley define the array, including its row and column size.
//             Structure uses a dynamic 2 dimensional array which is passed to
//             a function which returns the transpose of the matrix.
              
#include 
#include 
#include 

using namespace std;

//Matrix defined as a structure.
struct Matrix
{
       int** mat;
       int row;
       int column;
       };
//This funtion returns the transpose of a given matrix.
Matrix transpose(Matrix &a){
          int r=0, c=0;
          int** temp;
          
          r = a.column; // Set temp variables
          c = a.row;
          
          // Set the size of the temp array to the column size of a.
          temp = new int*[r]; 
       
       //Initialize the temp array.
       for (int k = 0; k < r; k++)
       temp[k] = new int[c];
        
        // Fill the temp array rows and columns with the columns and rows of a.
         for (int i =0; i< r; i++){
             for(int j=0; j < c; j++){
                temp[i][j] = a.mat[j][i]; //
                }
                }  
       //Change the actual row and column size of a
       a.column = c; 
       a.row = r;
          
       delete []a.mat; // Free up a.
          
       a.mat = new int*[r]; // Set a to its new dimensions
       
       for (int k = 0; k < r; k++)
       a.mat[k] = new int[c];
       
       a.mat = temp; // a = aT
                    
          return a;           
         }                         
               
int main()
{
 srand(time(0));
 Matrix a;
 
 // Input the row and column size.
 cout << "Enter a row size: ";
 cin >> a.row;
 cout << "Enter a column size: ";
 cin >> a.column;
 cout << endl;

 //Create an empty m x n matrix. 
 a.mat = new int*[a.row];
 
       for (int k = 0; k < a.row; k++)
       a.mat[k] = new int[a.column];
       
 // Add random values to the matrix.
 for (int i =0; i < a.row; i++){
       for(int j=0; j < a.column; j++){
                a.mat[i][j] = rand()%10; //Add random values 0-9
                }
                }
    //Print the Matrix.
    for (int i =0; i < a.row; i++){
       for(int j=0; j < a.column; j++){
                cout << setw(4) << a.mat[i][j];
                }
                cout << endl;
                }
    a = transpose(a); //Take the transpose of a. 
    cout << endl;
    
    //Print the Transpose.
    for (int i =0; i < a.row; i++){
        for(int j=0; j < a.column; j++){
        cout << setw(4) << a.mat[i][j];
                }
                cout << endl;
               }
  return 0;  
}