В двух заданных матрицах найти максимальные элементы и поменять их местами - C#

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

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

В двух заданных матрицах найти максимальные элементы и поменять их местами. Поиск максимального элемента матрицы оформить в виде метода Здравствуйте! Подскажите пожалуйста,что не так? При запуске, после того как выводит мои матрицы пишет "Индекс находился вне границ массива"
Листинг программы
  1. class Program
  2. {
  3. static void maxx(int[,] x, ref int xmax, ref int imax, ref int jmax)
  4. {
  5. int i, j;
  6. int n = x.Length;
  7. xmax = x[0, 0];
  8. imax = 0; jmax = 0;
  9. for (i = 0; i < n; i++)
  10. {
  11. for (j = 0; j < n; j++)
  12. if (x[i, j] >= xmax)
  13. {
  14. xmax = x[i, j];
  15. imax = i;
  16. jmax = j;
  17. }
  18. }
  19. }
  20.  
  21. static void Main()
  22. {
  23. int i, j, n, m;
  24. Console.WriteLine("Введите размеры матрицы");
  25. n = int.Parse(Console.ReadLine());
  26. m = int.Parse(Console.ReadLine());
  27. int[,] a = new int[n, m];
  28. int[,] b = new int[n, m];
  29. Console.WriteLine("Введите элементы матрицы 1");
  30. for (i = 0; i < n; i++)
  31. { for (j = 0; j < m; j++)
  32. a[i, j] = int.Parse(Console.ReadLine());
  33. }
  34. for (i = 0; i < n; i++)
  35. {
  36. for (j = 0; j < m; j++)
  37. Console.Write(" {0:d} ", a[i, j]);
  38. Console.WriteLine();
  39. }
  40. Console.WriteLine("Введите элементы матрицы 2");
  41. Console.WriteLine();
  42. for (i = 0; i < n; i++)
  43. {
  44. for (j = 0; j < m; j++)
  45. b[i, j] = int.Parse(Console.ReadLine());
  46. }
  47. for (i = 0; i < n; i++)
  48. {
  49. for (j = 0; j < m; j++)
  50. Console.Write(" {0:f0} ", b[i, j]);
  51. Console.WriteLine();
  52. }
  53.  
  54. Console.WriteLine();
  55. Console.WriteLine();
  56. int amax = 0; int imax = 0; int jmax = 0;
  57. int bmax = 0, ibmax = 0, jbmax = 0;
  58. maxx(a, ref amax, ref imax, ref jmax);
  59. maxx(b, ref bmax, ref ibmax, ref jbmax);
  60. a[imax, jmax] = bmax;
  61. b[ibmax, jbmax] = amax;
  62. for (i = 0; i < a.Length; i++)
  63. { for (j = 0; j < a.Length; j++)
  64. Console.Write("{0:d} ", a[i, j]);
  65. }
  66. Console.WriteLine();
  67. for (i = 0; i < b.Length; i++)
  68. { for (j = 0; j < b.Length; j++)
  69. Console.Write("{0:d} ", b[i, j]);
  70. }
  71. Console.WriteLine();
  72.  
  73. }
  74. }

Решение задачи: «В двух заданных матрицах найти максимальные элементы и поменять их местами»

textual
Листинг программы
  1. using System;
  2.  
  3. namespace ConsoleApp1
  4. {
  5.     class Program
  6.     {
  7.         static void Inp(int[,] a)
  8.         {
  9.             for (int i = 0; i < a.GetLength(0); i++)
  10.                 for (int j = 0; j < a.GetLength(1); j++)
  11.                 {
  12.                     Console.Write($"[{i + 1}, {j + 1}] = ");
  13.                     a[i, j] = int.Parse(Console.ReadLine());
  14.                 }
  15.         }
  16.  
  17.         static void Output(int[,] a)
  18.         {
  19.             for (int i = 0; i < a.GetLength(0); i++)
  20.             {
  21.                 for (int j = 0; j < a.GetLength(1); j++)
  22.                     Console.Write(a[i, j] + "\t");
  23.                 Console.WriteLine();
  24.             }
  25.             Console.WriteLine();
  26.         }
  27.  
  28.         static int imA = 0, jmA = 0, imB = 0, jmB = 0;
  29.  
  30.         static void SwapMax(int[,] a, int[,] b)
  31.         {
  32.             int x = a[imA, jmA];
  33.             a[imA, jmA] = b[imB, jmB];
  34.             b[imB, jmB] = x;
  35.         }
  36.  
  37.         static void Main()
  38.         {
  39.             Console.Write("n = "); int n = int.Parse(Console.ReadLine());
  40.             Console.Write("m = "); int m = int.Parse(Console.ReadLine());
  41.             int[,] a = new int[n, m];
  42.             int[,] b = new int[n, m];
  43.             Console.WriteLine("Матрица A:"); Inp(a);
  44.             Console.WriteLine("Матрица B:"); Inp(b);
  45.             Console.Clear();
  46.             Console.WriteLine("Матрица A:"); Output(a);
  47.             Console.WriteLine("Матрица B:"); Output(b);
  48.  
  49.             for (int i = 0; i < a.GetLength(0); i++)
  50.                 for (int j = 0; j < a.GetLength(1); j++)
  51.                     if (a[i, j] > a[imA, jmA]) { imA = i; jmA = j; }
  52.  
  53.             for (int i = 0; i < b.GetLength(0); i++)
  54.                 for (int j = 0; j < b.GetLength(1); j++)
  55.                     if (b[i, j] > b[imB, jmB]) { imB = i; jmB = j; }
  56.  
  57.             SwapMax(a, b);
  58.             Console.WriteLine("Результирующая A:"); Output(a);
  59.             Console.WriteLine("Результирующая B:"); Output(b);
  60.         }
  61.     }
  62. }

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


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

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

14   голосов , оценка 3.714 из 5

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

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

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