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 are nullable types in c#
What is lazy in c#?
What is difference between const and static in c#?
What is the meaning of 0 in c#?
What are virtual classes in c#?
What is lazy class in c#?
Can we have the method in drived class with the same name which is there in base class?
How?s the DLL Hell problem solved in .NET?
What’s thread.sleep() in threading ?
How many types of interface are there in c#?
What is difference between function and method in c#?
How does aspect oriented programming work?