Транспонирование прямоугольной матрицы - C#
Формулировка задачи:
Доброго времени суток. Задача написать консольное приложение для транспонирования матрицы. Размер матрицы n и m вводятся пользователем.
Проблема в том, что написанный код, транспонирует только КВАДРАТНУЮ матрицу. При прямоугольной выводит ошибку на строку.
Подскажите, пожалуйста, в чем ошибка?
static void transp(int [,] a)
{ int b;
for (int i = 0; i < a.GetLength(0); ++i, Console.WriteLine())
for (int j = 1; j < a.GetLength(1); ++j)
b= a[i,j];
a[i,j] = a[j, i];
a[j, i]= b;
}Решение задачи: «Транспонирование прямоугольной матрицы»
textual
Листинг программы
using System;
namespace std
{
class Program
{
static void Main()
{
Int32 n = 0, m = 0;
Console.Write("Число строк :");
n = Int32.Parse(Console.ReadLine());
Console.Write("Число столпцов :");
m = Int32.Parse(Console.ReadLine());
int[,] mas = new int[n, m];
Console.WriteLine("n = {0}, m = {1}", n, m);
init_rand(mas);
show(mas);
transp(mas);
Console.ReadKey();
}
static void init_rand(int[,] mas)
{
Random rand = new Random();
for(int i = 0; i < mas.GetLength(0); i++)
for (int j = 0; j < mas.GetLength(1); j++)
{
mas[i, j] = rand.Next(0, 100);
}
}
static void show(int[,] mas)
{
Console.WriteLine("*************************************");
for (int i = 0; i < mas.GetLength(0); i++)
{
for (int j = 0; j < mas.GetLength(1); j++)
{
Console.Write(mas[i, j] + "\t");
}
Console.WriteLine();
}
}
static void transp(int[,] mas)
{
int[,] temp = new int[mas.GetLength(1), mas.GetLength(0)];
for (int i = 0; i < temp.GetLength(0); i++)
{
for (int j = 0; j < temp.GetLength(1); j++)
{
temp[i,j] = mas[j, i];
}
Console.WriteLine();
}
for (int i = 0; i < temp.GetLength(0); i++)
{
for (int j = 0; j < temp.GetLength(1); j++)
{
Console.Write(temp[i, j]+ "\t");
}
Console.WriteLine();
}
}
}
}