Работа с матрицами (2d массивы) - C#

Узнай цену своей работы

Формулировка задачи:

часть кода моей л/р по проге (работа с массивами: ввод/вывод, max/min и т.п.)...
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace Array_Rang_2
  7. {
  8. class Program
  9. {
  10. int[] intArray = null;
  11. int inputChoice = 0;
  12. int inputChoiceLength = 0;
  13. bool exit = true;
  14. //Ввод матрицы с клав-ы
  15. void inputMatrix(int[] args)
  16. {
  17. //Console.WriteLine("Введите размерность матрицы: ");
  18. Console.Write("Введите кол-во строк матрицы: ");
  19. int m = Convert.ToInt32(Console.ReadLine()); //строки
  20. Console.Write("Введите кол-во столбцов матрицы: ");
  21. int n = Convert.ToInt32(Console.ReadLine()); //столбцы
  22. Console.WriteLine(" З А П О Л Н Е Н И Е ");
  23. int[,] arrMtrx = new int[m, n];
  24. for (int i = 0; i < m; i++)
  25. for (int j = 0; j < n; j++)
  26. {
  27. string s = Console.ReadLine();
  28. arrMtrx[i, j] = int.Parse(s);
  29. }
  30. for (int i = 0; i < m; i++)
  31. {
  32. for (int j = 0; j < n; j++)
  33. Console.Write("{0:d} ", arrMtrx[i, j]);
  34. Console.WriteLine();
  35. }
  36. Console.ReadLine();
  37. }
  38. // Max, Min матрицы // NE RABOTAET??!???
  39. void matrixMaxMin(int[] arrMtrx)
  40. {
  41. int rxMax = arrMtrx[0, 0]; int maxRow = 0; int maxCol = 0;
  42. int rxMin = arrMtrx[0, 0]; int minRow = 0; int minCol = 0;
  43. for (int i = 1; i < m; i++)
  44. for (int j = 1; j < n; j++)
  45. {
  46. if (arrMtrx[i, j] > rxMax)
  47. {
  48. maxRow = i; maxCol = j;
  49. rxMax = arrMtrx[i, j];
  50. }
  51. if (arrMtrx[i, j] < rxMin)
  52. {
  53. minRow = i; minCol = j;
  54. rxMin = arrMtrx[i, j];
  55. }
  56. }
  57. maxRow = maxRow + 1; maxCol = maxCol + 1; minRow = minRow + 1; minCol = minCol + 1;
  58. Console.WriteLine("Max матрицы: a[" + maxRow + "," + maxCol + "]=" + rxMax);
  59. Console.WriteLine("Min матрицы: a[" + minRow + "," + minCol + "]=" + rxMin);
  60. Console.ReadKey();
  61. }
  62. }
  63. }
в отдельном решении, все работало. а если переписать этот код в метод, то нет.

что не так, подскажите?

Решение задачи: «Работа с матрицами (2d массивы)»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace Array_Rang_2
  8. {
  9.     class Program
  10.     {
  11.         int[] intArray = null;
  12.         int[,] matrix = null;
  13.         int m,n;
  14.         int inputChoice = 0;
  15.         int inputChoiceLength = 0;
  16.         bool notnull = false;
  17.         bool notnullM = false;
  18.         bool exit = true;
  19.         public void inputArray()
  20.         {
  21.             Console.Write("Длина : ");
  22.             intArray = new int[Int32.Parse(Console.ReadLine())];
  23.             for (int i = 0; i < intArray.Length; i++)
  24.             {
  25.                 Console.Write("[{0}] = ",i);
  26.                 intArray[i] = Int32.Parse(Console.ReadLine());
  27.             }
  28.             Console.WriteLine();
  29.             foreach (int n in intArray)
  30.                 Console.Write(n + " ");
  31.             notnull = true;
  32.             Console.WriteLine();
  33.             Console.ReadKey(true);
  34.         }
  35.         public void MinMax()
  36.         {
  37.             if (notnull)
  38.             {
  39.                 int min = int.MaxValue, max = int.MinValue, minI = 0, maxI = 0;
  40.                 for (int i = 0; i < intArray.Length; i++)
  41.                 {
  42.                     if (intArray[i] > max)
  43.                     {
  44.                         max = intArray[i];
  45.                         maxI = i;
  46.                     }
  47.                     if (intArray[i] < min)
  48.                     {
  49.                         min = intArray[i];
  50.                         minI = i;
  51.                     }
  52.                 }
  53.                 Console.WriteLine("min : [{0}] = {1}", minI, min);
  54.                 Console.WriteLine("max : [{0}] = {1}", maxI, max);
  55.             }
  56.             else Console.WriteLine("массив не заполнен!!");
  57.             Console.WriteLine();
  58.             Console.ReadKey(true);
  59.         }
  60.         public void Show()
  61.         {
  62.             if (notnull)
  63.             {
  64.                 foreach (int n in intArray)
  65.                     Console.Write(n + " ");
  66.             }
  67.             else Console.WriteLine("массив не заполнен!!");
  68.             Console.WriteLine();
  69.             Console.ReadKey(true);
  70.         }
  71.         //Ввод матрицы с клав-ы
  72.         public void inputMatrix()
  73.         {
  74.             //Console.WriteLine("Введите размерность матрицы: ");
  75.             Console.Write("Введите кол-во строк матрицы: ");
  76.             m = Convert.ToInt32(Console.ReadLine()); //строки
  77.             Console.Write("Введите кол-во столбцов матрицы: ");
  78.             n = Convert.ToInt32(Console.ReadLine()); //столбцы
  79.             Console.WriteLine(" З А П О Л Н Е Н И Е ");
  80.             matrix = new int[m, n];
  81.             for (int i = 0; i < m; i++)
  82.                 for (int j = 0; j < n; j++)
  83.                 {
  84.                     Console.Write("[{0},{1}] = ",i,j);
  85.                     matrix[i, j] = int.Parse(Console.ReadLine());
  86.                 }
  87.             for (int i = 0; i < m; i++)
  88.             {
  89.                 for (int j = 0; j < n; j++)
  90.                     Console.Write("{0:d} ", matrix[i, j]);
  91.                 Console.WriteLine();
  92.             }
  93.             notnullM = true; ;
  94.             Console.ReadKey(true);
  95.         }
  96.  
  97.           public void matrixMaxMin(int[,] matrix)
  98.           {
  99.               if (notnullM)
  100.               {
  101.                   int rxMax = matrix[0, 0]; int maxRow = 0; int maxCol = 0;
  102.                   int rxMin = matrix[0, 0]; int minRow = 0; int minCol = 0;
  103.                   for (int i = 1; i < m; i++)
  104.                       for (int j = 1; j < n; j++)
  105.                       {
  106.                           if (matrix[i, j] > rxMax)
  107.                           {
  108.                               maxRow = i; maxCol = j;
  109.                               rxMax = matrix[i, j];
  110.                           }
  111.                           if (matrix[i, j] < rxMin)
  112.                           {
  113.                               minRow = i; minCol = j;
  114.                               rxMin = matrix[i, j];
  115.  
  116.                           }
  117.                       }
  118.                   maxRow = maxRow + 1; maxCol = maxCol + 1; minRow = minRow + 1; minCol = minCol + 1;
  119.                   Console.WriteLine("Max матрицы: a[" + maxRow + "," + maxCol + "]=" + rxMax);
  120.                   Console.WriteLine("Min матрицы: a[" + minRow + "," + minCol + "]=" + rxMin);
  121.               }
  122.               else Console.WriteLine("Матрица не заполнена !!!");
  123.               Console.ReadKey(true);
  124.           }
  125.  
  126.  
  127.         //static void menu()
  128.         void menu()
  129.         {
  130.             while (exit)
  131.             {
  132.                 Console.SetCursorPosition(1, 1);
  133.                 Console.BackgroundColor = ConsoleColor.DarkMagenta;
  134.                 Console.ForegroundColor = ConsoleColor.White;
  135.                 Console.Write("Выберите опцию: \n ---МАССИВЫ---\n 1 - ввод массива\n 2 - min/max\n 3 - вывод\n 4 - отсортировать по возрастанию\n  ---МАТРИЦЫ--- \n 7 - ввод матрицы \n 8 - max, min элемент матрицы \n");
  136.                 Console.ResetColor();
  137.                 Console.Write(">> ");
  138.                 inputChoice = Convert.ToInt32(Console.ReadLine());
  139.                 //Console.WriteLine("inputChoice = " + inputChoice);
  140.                 switch (inputChoice)
  141.                 {
  142.                     case 1: inputArray();
  143.                         break;
  144.                     case 2: MinMax();
  145.                         break;
  146.                     case 3: Show();
  147.                         break;
  148.                     case 4: Console.WriteLine(@"\_('_')_/");
  149.                         break;
  150.                     case 7: inputMatrix();
  151.                         break;
  152.                     case 8: matrixMaxMin(matrix);
  153.                         break;
  154.                     case 0: exit = false;
  155.                         break;
  156.                 }
  157.                 if (exit == true)
  158.                 {
  159.                     Console.WriteLine("Нажмите любую клавишу для продолжения...");
  160.                     Console.ReadKey(true);
  161.                 }
  162.                 Console.Clear();
  163.             }
  164.         }
  165.  
  166.         static void Main(string[] args)
  167.         {
  168.             Program p = new Program();
  169.             for (; ; )
  170.                 p.menu();
  171.         }
  172.     }
  173. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

9   голосов , оценка 3.889 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы