Работа с матрицами (2d массивы) - C#
Формулировка задачи:
часть кода моей л/р по проге (работа с массивами: ввод/вывод, max/min и т.п.)...
в отдельном решении, все работало. а если переписать этот код в метод, то нет.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Array_Rang_2
{
class Program
{
int[] intArray = null;
int inputChoice = 0;
int inputChoiceLength = 0;
bool exit = true;
//Ввод матрицы с клав-ы
void inputMatrix(int[] args)
{
//Console.WriteLine("Введите размерность матрицы: ");
Console.Write("Введите кол-во строк матрицы: ");
int m = Convert.ToInt32(Console.ReadLine()); //строки
Console.Write("Введите кол-во столбцов матрицы: ");
int n = Convert.ToInt32(Console.ReadLine()); //столбцы
Console.WriteLine(" З А П О Л Н Е Н И Е ");
int[,] arrMtrx = new int[m, n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
{
string s = Console.ReadLine();
arrMtrx[i, j] = int.Parse(s);
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
Console.Write("{0:d} ", arrMtrx[i, j]);
Console.WriteLine();
}
Console.ReadLine();
}
// Max, Min матрицы // NE RABOTAET??!???
void matrixMaxMin(int[] arrMtrx)
{
int rxMax = arrMtrx[0, 0]; int maxRow = 0; int maxCol = 0;
int rxMin = arrMtrx[0, 0]; int minRow = 0; int minCol = 0;
for (int i = 1; i < m; i++)
for (int j = 1; j < n; j++)
{
if (arrMtrx[i, j] > rxMax)
{
maxRow = i; maxCol = j;
rxMax = arrMtrx[i, j];
}
if (arrMtrx[i, j] < rxMin)
{
minRow = i; minCol = j;
rxMin = arrMtrx[i, j];
}
}
maxRow = maxRow + 1; maxCol = maxCol + 1; minRow = minRow + 1; minCol = minCol + 1;
Console.WriteLine("Max матрицы: a[" + maxRow + "," + maxCol + "]=" + rxMax);
Console.WriteLine("Min матрицы: a[" + minRow + "," + minCol + "]=" + rxMin);
Console.ReadKey();
}
}
}что не так, подскажите?
Решение задачи: «Работа с матрицами (2d массивы)»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Array_Rang_2
{
class Program
{
int[] intArray = null;
int[,] matrix = null;
int m,n;
int inputChoice = 0;
int inputChoiceLength = 0;
bool notnull = false;
bool notnullM = false;
bool exit = true;
public void inputArray()
{
Console.Write("Длина : ");
intArray = new int[Int32.Parse(Console.ReadLine())];
for (int i = 0; i < intArray.Length; i++)
{
Console.Write("[{0}] = ",i);
intArray[i] = Int32.Parse(Console.ReadLine());
}
Console.WriteLine();
foreach (int n in intArray)
Console.Write(n + " ");
notnull = true;
Console.WriteLine();
Console.ReadKey(true);
}
public void MinMax()
{
if (notnull)
{
int min = int.MaxValue, max = int.MinValue, minI = 0, maxI = 0;
for (int i = 0; i < intArray.Length; i++)
{
if (intArray[i] > max)
{
max = intArray[i];
maxI = i;
}
if (intArray[i] < min)
{
min = intArray[i];
minI = i;
}
}
Console.WriteLine("min : [{0}] = {1}", minI, min);
Console.WriteLine("max : [{0}] = {1}", maxI, max);
}
else Console.WriteLine("массив не заполнен!!");
Console.WriteLine();
Console.ReadKey(true);
}
public void Show()
{
if (notnull)
{
foreach (int n in intArray)
Console.Write(n + " ");
}
else Console.WriteLine("массив не заполнен!!");
Console.WriteLine();
Console.ReadKey(true);
}
//Ввод матрицы с клав-ы
public void inputMatrix()
{
//Console.WriteLine("Введите размерность матрицы: ");
Console.Write("Введите кол-во строк матрицы: ");
m = Convert.ToInt32(Console.ReadLine()); //строки
Console.Write("Введите кол-во столбцов матрицы: ");
n = Convert.ToInt32(Console.ReadLine()); //столбцы
Console.WriteLine(" З А П О Л Н Е Н И Е ");
matrix = new int[m, n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
{
Console.Write("[{0},{1}] = ",i,j);
matrix[i, j] = int.Parse(Console.ReadLine());
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
Console.Write("{0:d} ", matrix[i, j]);
Console.WriteLine();
}
notnullM = true; ;
Console.ReadKey(true);
}
public void matrixMaxMin(int[,] matrix)
{
if (notnullM)
{
int rxMax = matrix[0, 0]; int maxRow = 0; int maxCol = 0;
int rxMin = matrix[0, 0]; int minRow = 0; int minCol = 0;
for (int i = 1; i < m; i++)
for (int j = 1; j < n; j++)
{
if (matrix[i, j] > rxMax)
{
maxRow = i; maxCol = j;
rxMax = matrix[i, j];
}
if (matrix[i, j] < rxMin)
{
minRow = i; minCol = j;
rxMin = matrix[i, j];
}
}
maxRow = maxRow + 1; maxCol = maxCol + 1; minRow = minRow + 1; minCol = minCol + 1;
Console.WriteLine("Max матрицы: a[" + maxRow + "," + maxCol + "]=" + rxMax);
Console.WriteLine("Min матрицы: a[" + minRow + "," + minCol + "]=" + rxMin);
}
else Console.WriteLine("Матрица не заполнена !!!");
Console.ReadKey(true);
}
//static void menu()
void menu()
{
while (exit)
{
Console.SetCursorPosition(1, 1);
Console.BackgroundColor = ConsoleColor.DarkMagenta;
Console.ForegroundColor = ConsoleColor.White;
Console.Write("Выберите опцию: \n ---МАССИВЫ---\n 1 - ввод массива\n 2 - min/max\n 3 - вывод\n 4 - отсортировать по возрастанию\n ---МАТРИЦЫ--- \n 7 - ввод матрицы \n 8 - max, min элемент матрицы \n");
Console.ResetColor();
Console.Write(">> ");
inputChoice = Convert.ToInt32(Console.ReadLine());
//Console.WriteLine("inputChoice = " + inputChoice);
switch (inputChoice)
{
case 1: inputArray();
break;
case 2: MinMax();
break;
case 3: Show();
break;
case 4: Console.WriteLine(@"\_('_')_/");
break;
case 7: inputMatrix();
break;
case 8: matrixMaxMin(matrix);
break;
case 0: exit = false;
break;
}
if (exit == true)
{
Console.WriteLine("Нажмите любую клавишу для продолжения...");
Console.ReadKey(true);
}
Console.Clear();
}
}
static void Main(string[] args)
{
Program p = new Program();
for (; ; )
p.menu();
}
}
}