How to transpose rows into columns and columns into rows in
a multi-dimensional array?

Answers were Sorted based on User's Feedback



How to transpose rows into columns and columns into rows in a multi-dimensional array?..

Answer / sarath

We Have a predefined method for transposing Matrix i.e
TRANSPOSE().


for ex: We have one Matrix called 'A'

A is the array | 0 -5 8 -7 |
| 2 4 -1 1 |
| 7 5 6 -6 |
Transpose the columns and rows of A.
RES = TRANSPOSE( A )
The result is | 0 2 7 |
| -5 4 5 |
| 8 -1 6 |
! | -7 1 -6 |

Is This Answer Correct ?    21 Yes 5 No

How to transpose rows into columns and columns into rows in a multi-dimensional array?..

Answer / raji

int [,]array=new int[2,2] {{1,2},{3,4}};

transpose of the array is

for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
Console.Write("{0} ",a[j,i]);
}
}

o/p : 1 3
2 4

Is This Answer Correct ?    20 Yes 7 No

How to transpose rows into columns and columns into rows in a multi-dimensional array?..

Answer / venugopal

//Here 'i' indicates for 'i'th row and 'j' indicates
for 'j'th row
//Thus, the matrix is iXj with 3X2 size
//The result matrix should be 2X3

//The logic is as fallows

for(int j=0;j<2;J++)
{
for(int i=0;i<3;i++)
console.write(a[j,i]);
//After completing the first row it should be in the
next line
console.writeln("\n");
}

The final out put should be

2 1 3
2 2 4

Is This Answer Correct ?    8 Yes 3 No

How to transpose rows into columns and columns into rows in a multi-dimensional array?..

Answer / vinod rawal

using System;
using System.Collections.Generic;
using System.Text;

// C# code for Transpose Of Matrix (C Sharp) ( Dot net)



namespace TransposeOfMatrix

{

///

/// Summary description for Class1.

///

class Class1

{

public static Class1 cs;

public static int s=0,m=0;

///

/// The main entry point for the application.

///

[STAThread]

static void Main(string[] args)

{

//

// TODO: Add code to start application here

//

int [,]a=new int[10,10];

cs=new Class1();

Console.Write("Enter the order of First Matrix : ");

s=int.Parse(Console.ReadLine());

Console.Write("- ");

m=int.Parse(Console.ReadLine());

Console.WriteLine();

Console.WriteLine("\nEnter The value of First Matrice:");

cs.matrice(a,s,m);

Console.WriteLine("Matrix entered is:\n");

cs.arrange(s);

cs.arrange(a,s,m);

cs.arrange(s);

Console.WriteLine("Transpose of Matrix is :\n");

cs.transpose(a,s,m);

Console.ReadLine();

}

public void matrice(int [,]c,int k,int l)

{

for(int i=0;i<=k-1;i++)

{

for(int j=0;j<=l-1;j++)

{

c[i,j]=int.Parse(Console.ReadLine());

}

}

}

public void arrange(int [,]c,int k,int l)

{

for(int i=0;i<=k-1;i++)

{

for(int j=0;j<=l-1;j++)

{

Console.Write(c[i,j]+"\t");

}

Console.WriteLine();

}

}

public void transpose(int [,]c,int s,int m)

{

int [,]d=new int[10,10];

for(int i=0;i<=s-1;i++)

{

for(int j=0;j<=m-1;j++)

{

d[j,i]=c[i,j];

}

}

cs.arrange(s);

cs.arrange(d,m,s);

cs.arrange(s);

}

public void arrange(int x)

{

for(int i=0;i<=x;i++)

{

Console.Write("----------");

}

Console.WriteLine();

}

}

}

Is This Answer Correct ?    2 Yes 3 No

Post New Answer

More C Sharp Interview Questions

Explain the concepts of cts and cls(common language specification).

0 Answers  


What is the use of getcommandlineargs() method in c#.net?

0 Answers  


What is the difference between // comments, /* */ comments and /// comments?

0 Answers  


Why do we use polymorphism in c#?

0 Answers  


What is difference between array and arraylist in c#?

0 Answers  


What is difference between class and interface in c#?

0 Answers  


How do switch statements work?

0 Answers  


what is the fastest way to concatenate strings in c sharp?

4 Answers   HCL,


can we throw execption from catchblock

4 Answers   Accenture,


What is the delegates in c#?

0 Answers  


What sort algorithm does c# use?

0 Answers  


Are c# destructors the same as c++ destructors?

0 Answers  


Categories