Yatra.com Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
Can be done by swapping elements one by one of the 1st row and 1st column and then recursively passing the rest of the matrix (excluding 1st row and 1st column).
// rs -row start
// cs - column start
// re - row end
// ce - col end
3 void transposeMat(int** a, int rs, int cs, int re, int ce) {
4 int rows = re-rs;
5 if (rows==0)
6 return;
7
8 int i, t;
9 for (i=rs; i<=rows; i++) {
10 t = a[i][cs];
11 a[i][cs] = a[cs][i];
12 a[cs][i] = t;
13 }
14
15 transposeMat(a, rs+1, cs+1, re, ce);
16 }
when a matrix is non-square, we might run into loops when swapping the elements..
the following soln works:
1. assume matrix is stored as 1D array
2. for each element in the resulting (transposed) matrix, say its index is 'k', find the index of a corresponding element in the original matrix, say it's 'j'
3. if j > k then swap(a[j], a[k])
4. otherwise we should "follow the loop" since element in the location 'j' has
already been swapped
I hope my obscure explanations are understandable ))
here is the code:
// in: n rows; m cols
// out: n cols; m rows
void matrix_transpose(int *a, int n, int m) {
int i, j;
printf("\nin: %d x %d\n", n, m);
for(i = 0; i < n; i++) {
for(j = 0; j < m; j++)
printf("%d ", a[i*m + j]);
printf("\n");
}
for(int k = 0; k < n*m; k++) {
int idx = k;
do { // calculate index in the original array
idx = (idx % n) * m + (idx / n);
} while(idx < k);
std::swap(a[k], a[idx]);
}
printf("\nout: %d x %d\n", m, n);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++)
printf("%d ", a[i*n + j]);
printf("\n");
}
}
int main() {
int n = 2, m = 8; // n rows, m cols
int a[] = {1,1,1,1, 2,2,2,2, 3,3,3,3, 4,4,4,4};
matrix_transpose(a, n, m);
return 1;
}
I used transform and coding for this problem.
When the matrix is a square, it's a trivial job to transform. i.e. by swapping lower and upper triangular matrix.
When the matrix is a non square, I'll get the square matrix out of it, i.e. if it is a '4X2' matrix, I'll apply the above method for '2X2' and I will be left to transform 3rd and 4th row. So you can interchange the row and column number to get the solution
Here is a code which implements the same
#include<iostream>
using namespace std;
const int m = 3; //Number of rows
const int n = 10; //Number of columns
int main()
{
int a[10][10] = {
{ 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, 26, 27, 28, 29, 30 }
};
int i, j;
cout << "MATRIX A : \n";
for( i = 0; i < m; i++ )
{
for( j = 0; j < n; j++ )
cout << a[i][j] << "\t";
cout << endl;
}
// Transpose begins here
int index, m_index = 0, n_index = 0;
if( m == n )
index = n;
else if( m < n )
m_index = index = m;
else
n_index = index = n;
for( i = 0; i < index-1; i++ )
{
for( j = i+1; j < index; j++ )
{
int temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}
if( m_index == 0 ) // implies n < m
{
for( i = n; i < m; i++ )
for( j = 0; j < n; j++ )
{
cout << " i : " << i << " j : " << j << endl;
a[j][i] = a[i][j];
a[i][j] = 0;
}
}
else if( n_index == 0 ) // implies m < n
{
for( i = m; i < n; i++ )
for( j = 0; j < m; j++ )
{
a[i][j] = a[j][i];
a[j][i] = 0;
}
}
cout << "\nTRANSPOSE OF A : \n";
for( i = 0; i < n; i++ )
{
for( j = 0; j < m; j++ )
cout << a[i][j] << "\t";
cout << endl;
}
return 0;
}
- mag January 25, 2012