/* transpose.cpp - Transpose a square array */

#include <iostream>

enum {TABLESIZE=5};

// transpose the TABLESIZEXTABLESIZE subarray of the array a 
void transpose(int a[][TABLESIZE])
{  
   int temp;

   for (int row=1; row<TABLESIZE; row++)
   {
      for (int clmn=0; clmn<row; clmn++)
      {
         temp = a[row][clmn];
         a[row][clmn] = a[clmn][row];
         a[clmn][row] = temp;
      }
   }
}

// print out the first n rows of nxTABLESIZE array a
void printArray(int a[][TABLESIZE], int n)
{
  for (int row=0; row<n; row++)
  {
     for (int clmn=0; clmn<TABLESIZE; clmn++)
     {
        cout << '\t' << a[row][clmn];
     }
     cout << endl;
  }
}

void main (void)
{ 
  int table[TABLESIZE][TABLESIZE] =
         { {1,2,3,4,5},
           {6,7,8,9,10},
           {11,12,13,14,15},
           {16,17,18,19,20},
           {21,22,23,24,25}};

  cout << "before transpose" << endl;
  printArray(table, TABLESIZE);

  transpose(table);

  cout << "\nafter transpose" << endl;
  printArray(table, TABLESIZE);
}

/* The output is:

before transpose
	1	2	3	4	5
	6	7	8	9	10
	11	12	13	14	15
	16	17	18	19	20
	21	22	23	24	25

after transpose
	1	6	11	16	21
	2	7	12	17	22
	3	8	13	18	23
	4	9	14	19	24
	5	10	15	20	25

*/
