Cдвиг вправо или влево на n элементов в матрице - C#
Формулировка задачи:
Вот начало моего кода, помогите, пожалуйста
using System;
using System.Collections.Generic;
using System.Text;
namespace LabRab5
{
class Program
{
static void Main(string[] args)
{
//Массив элементов
Console.WriteLine("Введите кол-во строк:");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Введите кол-во столбцов:");
int k = Convert.ToInt32(Console.ReadLine());
int[,] a = new int[n, k];
Random random = new Random();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < k; j++)
{
a[i, j] = random.Next(50);
Console.Write("{0,4}", a[i, j]);
}
Console.WriteLine();
}
//Вводим количество элементов для сдвига
Console.Write("Введите количество элементов для сдвига: ");
string buf1 = Console.ReadLine();
//Ввдим режим сдвига
Console.Write("Выберите режим сдвига( 1 - вправо, 2 - вниз ): ");
string buf2 = Console.ReadLine();
int sd = int.Parse(buf2);
//Осуществляем сдвигиРешение задачи: «Cдвиг вправо или влево на n элементов в матрице»
textual
Листинг программы
static void Main(string[] args)
{
int[,] array =
{
{1,2,3 },
{4,5,6 },
{7,8,9 }
};
PrintArray(array);
PrintArray(MoveRight(array, 1));
PrintArray(MoveRight(array, -1));
Console.ReadKey();
}
static T[,] MoveRight<T>(T[,] array, int positions)
{
int l1 = array.GetLength(0), l2 = array.GetLength(1);
T[,] result = array.Clone() as T[,];
for (int i = 0; i < l1; i++)
{
for (int j = 0; j < l2; j++)
{
int jj = (j - positions) % l2;
jj = jj < 0 ? jj + l2 : jj;
result[i, j] = array[i, jj];
}
}
return result;
}
static void PrintArray<T>(T[,] array)
{
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
Console.Write(array[i, j].ToString() + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}