Преобразование типа из "int[*,*]" в "int[]" невозможно - C#
Формулировка задачи:
static int MatrixInput(string str = " ")
{
Console.WriteLine(str);
int x;
return x = Convert.ToInt32(Console.ReadLine());
}
static Random Random()
{
Random rnd = new Random();
return rnd;
}
// Для одномерного массива
static int [] FillMatrix (int n, Random rnd)
{
int[]A=new int[n];
for (int i = 0; i < A.Length; i++)
{
A[i] = rnd.Next(-10, 10);
}
return A;
}
static void MatrixOutPut(int[] A)
{
for (int i = 0; i < A.Length; i++)
{
Console.Write(A[i] + "\t");
}
Console.WriteLine();
}
static void Search(int[] A)
{
for (int i = 0; i < A.Length; i++)
{
if (A[i] < 0)
{
int n = A.Length;
while (n != 0)
{
if (A[n - 1] < 0)
{
Console.WriteLine("Наибольший индекс отрицательного элемента: "+(n - 1));
break;
}
else n--;
}
}
}
}
//для двумерных
static int[,] FillMatrix(int a, int b, Random rnd)
{
int[,]A=new int[a,b];
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < A.GetLength(1); j++)
{
A[i, j] = rnd.Next(-10, 10);
}
}
return A;
}
static void MatrixOutPut(int[,]A, char c)
{
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < A.GetLength(1); j++)
{
Console.Write(A[i, j] +c );
}
Console.WriteLine();
}
}
static void Search(int[,] A)
{
Console.WriteLine("Введите номер столбца, где будем искать отрицательные элементы:");
int j1 = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < A.GetLength(0); i++)
{
if (A[i, j1] < 0)
{
int w = A.GetLength(0);
while (w != 0)
{
if (A[w - 1, j1] < 0)
{
Console.WriteLine("Наибольший индекс отрицательного элемента "+(w - 1));
break;
}
else w--;
}
}
}
}
static void Main(string[] args)
{
// для одномерного
int[] m = FillMatrix(MatrixInput("Введите размер одномерного массива "), Random());
MatrixOutPut(m);
Search(m);
// для двумерного
int[,] p =FillMatrix(MatrixInput("Введите количество строк "), MatrixInput("Введите количество столбцов "), Random());
Search(p);
MatrixOutPut(p);
Console.ReadKey();
}
}
}
неактуально
Решение задачи: «Преобразование типа из "int[*,*]" в "int[]" невозможно»
textual
Листинг программы
MatrixOutPut(p)