How to transpose rows into columns and columns into rows in
a multi-dimensional array?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
What does the keyword “virtual†declare for a method or property?
Define MSIL, and how does it works? Why our developers need an appreciation of it if at all?
What are the types of inheritance in c#?
What is state c#?
What is toint32 c#?
What is the usage of transponders?
Can you instantiate a struct without using a new operator in c#?
What's difference between constants and static readonly?
What is value type and reference type?
what's the Difference between DataView and DataTable?
What are object pooling and connection pooling and difference? Where do we set the Min and Max Pool size for connection pooling?
What is the usage of OLE?
Visual Basic (800)
C Sharp (3816)
ASP.NET (3180)
VB.NET (461)
COM+ (79)
ADO.NET (717)
IIS (369)
MTS (11)
Crystal Reports (81)
BizTalk (89)
Dot Net (2435)
Exchange Server (362)
SharePoint (720)
WCF (340)
MS Office Microsoft (6963)
LINQ Language-Integrated Query (317)
WPF (371)
TypeScript (144)
Microsoft Related AllOther (311)