Вот так даже правильнее
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NoNamespace
{
internal static class Program
{
private static void Main()
{
Console.WriteLine("Введите n и m");
var n = int.Parse(Console.ReadLine());
var m = int.Parse(Console.ReadLine());
if ((n <= 0) || (m <= 0))<br> return;
var r = new Random(DateTime.Now.Millisecond);
Console.WriteLine(@"Task1");
var a = new int[n].ChangeEach(x => r.Next(100));
Console.WriteLine(a.Print());
Console.WriteLine(a.Where(x => x%2 == 1).Print());
Console.WriteLine();
Console.WriteLine(@"Task2");
var b = new int[n, m].ChangeEach(x => r.Next(-100, 100));
Console.WriteLine(b.Print());
Console.WriteLine(Task2(b).Print());
Console.WriteLine();
Console.WriteLine(@"Task3");
Console.WriteLine(b.Print());
Console.WriteLine(b.Where(x => (x < 0) && (Math.Abs(x%2) == 1)).Print());
Console.WriteLine();
Console.ReadLine();
}
private static int[,] Task2(int[,] a)
{
var max = a.MaxIndex((x, y) => x > y);
var maxv = a[max.Item1, max.Item2];
a[max.Item1, max.Item2] = a[0, 0];
a[0, 0] = maxv;
return a;
}
}
public static class Extensions
{
public static List Where(this T[,] array, Func filter)
{
if (filter == null)
return null;
var list = new List();
for (var i = 0; i < array.GetLength(1); i++)
for (var j = 0; j < array.GetLength(0); j++)
if (filter(array[i, j]))
list.Add(array[i, j]);
return list;
}
public static string Print(this IEnumerable a)
{
var enumerable = a as IList ?? a.ToList();
if (!enumerable.Any())
return "Таких элементов нет";
var sb = new StringBuilder("Массив: ");
foreach (var ae in enumerable)
sb.Append($"{ae} ");
return sb.ToString();
}
public static T[,] ChangeEach(this T[,] array, Func mutator)
{
if (mutator == null)
return array;
for (var i = 0; i < array.GetLength(1); i++)
for (var j = 0; j < array.GetLength(0); j++)
array[i, j] = mutator(array[i, j]);
return array;
}
public static T[] ChangeEach(this T[] array, Func mutator)
{
if (mutator == null)
return array;
for (var j = 0; j < array.GetLength(0); j++)
array[j] = mutator(array[j]);
return array;
}
public static string Print(this T[,] a)
{
if (a.LongLength == 0)
return "Таких элементов нет";
var sb = new StringBuilder("Матрица: \n");
for (var i = 0; i < a.GetLength(1); i++)
{
for (var j = 0; j < a.GetLength(0); j++)
sb.Append($"{a[i, j]} ");
sb.AppendLine();
}
return sb.ToString();
}
public static Tuple MaxIndex(this T[,] array, Func comparer)
{
if (comparer == null)
return new Tuple(-1, -1);
var index = new Tuple(0, 0);
for (var i = 0; i < array.GetLength(1); i++)
for (var j = 0; j < array.GetLength(0); j++)
if (comparer(array[i, j], array[index.Item1, index.Item2]))
index = new Tuple(i, j);
return index;
}
}
}