-
Notifications
You must be signed in to change notification settings - Fork 0
/
2D_array_Transpose.cs
63 lines (57 loc) · 1.43 KB
/
2D_array_Transpose.cs
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
namespace Array_2D
{
internal static class Program
{
private static void Main()
{
var a = new int[3, 5] {{5, 12, 17, 9, 3}, {13, 4, 8, 14, 1}, {9, 6, 3, 7, 21}};
for (var i = 0; i < 3; i++)
{
for (var j = 0; j < 5; j++)
{
// Print out each element
Console.Write(+a[i, j] + ",");
}
Console.WriteLine();
// Print new line character after the row is printed in above loop
}
var rowCount = a.GetLength(0);
var columnCount = a.GetLength(1);
var transpose = new int[columnCount, rowCount];
if (rowCount == columnCount)
{
transpose = (int[,]) a.Clone();
for (var i = 1; i < rowCount; i++)
{
for (var j = 0; j < i; j++)
{
var temp = transpose[i, j];
transpose[i, j] = transpose[j, i];
transpose[j, i] = temp;
}
}
}
else
{
for (var i = 0; i < columnCount; i++)
{
for (var j = 0; j < rowCount; j++)
{
transpose[i, j] = a[j, i];
}
}
}
for (var i = 0; i < 5; i++)
{
for (var j = 0; j < 3; j++)
{
// Print out each element
Console.Write(+transpose[i, j] + ",");
}
Console.WriteLine();
// Print new line character after the row is printed in above loop
}
}
}
}