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

#include <iostream>

enum {TABLESIZE=5};

// transpose the nxn subarray of the array a 
void transpose(int a[], int n)
{  
   int temp;

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

// print out an nxn array a
void printArray(int a[], int n)
{
  for (int row=0; row<n; row++)
  {
     for (int clmn=0; clmn<TABLESIZE; clmn++)
     {
        cout << '\t' << a[clmn + n*row];
     }
     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((int *)table, TABLESIZE);

  transpose((int *)table, TABLESIZE);

  cout << "\nafter transpose" << endl;
  printArray((int *)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

*/
