В двух заданных матрицах найти максимальные элементы и поменять их местами - C#
Формулировка задачи:
В двух заданных матрицах найти максимальные элементы и поменять их местами. Поиск максимального элемента матрицы оформить в виде метода
Здравствуйте! Подскажите пожалуйста,что не так? При запуске, после того как выводит мои матрицы пишет "Индекс находился вне границ массива"
Листинг программы
- class Program
- {
- static void maxx(int[,] x, ref int xmax, ref int imax, ref int jmax)
- {
- int i, j;
- int n = x.Length;
- xmax = x[0, 0];
- imax = 0; jmax = 0;
- for (i = 0; i < n; i++)
- {
- for (j = 0; j < n; j++)
- if (x[i, j] >= xmax)
- {
- xmax = x[i, j];
- imax = i;
- jmax = j;
- }
- }
- }
- static void Main()
- {
- int i, j, n, m;
- Console.WriteLine("Введите размеры матрицы");
- n = int.Parse(Console.ReadLine());
- m = int.Parse(Console.ReadLine());
- int[,] a = new int[n, m];
- int[,] b = new int[n, m];
- Console.WriteLine("Введите элементы матрицы 1");
- for (i = 0; i < n; i++)
- { for (j = 0; j < m; j++)
- a[i, j] = int.Parse(Console.ReadLine());
- }
- for (i = 0; i < n; i++)
- {
- for (j = 0; j < m; j++)
- Console.Write(" {0:d} ", a[i, j]);
- Console.WriteLine();
- }
- Console.WriteLine("Введите элементы матрицы 2");
- Console.WriteLine();
- for (i = 0; i < n; i++)
- {
- for (j = 0; j < m; j++)
- b[i, j] = int.Parse(Console.ReadLine());
- }
- for (i = 0; i < n; i++)
- {
- for (j = 0; j < m; j++)
- Console.Write(" {0:f0} ", b[i, j]);
- Console.WriteLine();
- }
- Console.WriteLine();
- Console.WriteLine();
- int amax = 0; int imax = 0; int jmax = 0;
- int bmax = 0, ibmax = 0, jbmax = 0;
- maxx(a, ref amax, ref imax, ref jmax);
- maxx(b, ref bmax, ref ibmax, ref jbmax);
- a[imax, jmax] = bmax;
- b[ibmax, jbmax] = amax;
- for (i = 0; i < a.Length; i++)
- { for (j = 0; j < a.Length; j++)
- Console.Write("{0:d} ", a[i, j]);
- }
- Console.WriteLine();
- for (i = 0; i < b.Length; i++)
- { for (j = 0; j < b.Length; j++)
- Console.Write("{0:d} ", b[i, j]);
- }
- Console.WriteLine();
- }
- }
Решение задачи: «В двух заданных матрицах найти максимальные элементы и поменять их местами»
textual
Листинг программы
- using System;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Inp(int[,] a)
- {
- for (int i = 0; i < a.GetLength(0); i++)
- for (int j = 0; j < a.GetLength(1); j++)
- {
- Console.Write($"[{i + 1}, {j + 1}] = ");
- a[i, j] = int.Parse(Console.ReadLine());
- }
- }
- static void Output(int[,] a)
- {
- for (int i = 0; i < a.GetLength(0); i++)
- {
- for (int j = 0; j < a.GetLength(1); j++)
- Console.Write(a[i, j] + "\t");
- Console.WriteLine();
- }
- Console.WriteLine();
- }
- static int imA = 0, jmA = 0, imB = 0, jmB = 0;
- static void SwapMax(int[,] a, int[,] b)
- {
- int x = a[imA, jmA];
- a[imA, jmA] = b[imB, jmB];
- b[imB, jmB] = x;
- }
- static void Main()
- {
- Console.Write("n = "); int n = int.Parse(Console.ReadLine());
- Console.Write("m = "); int m = int.Parse(Console.ReadLine());
- int[,] a = new int[n, m];
- int[,] b = new int[n, m];
- Console.WriteLine("Матрица A:"); Inp(a);
- Console.WriteLine("Матрица B:"); Inp(b);
- Console.Clear();
- Console.WriteLine("Матрица A:"); Output(a);
- Console.WriteLine("Матрица B:"); Output(b);
- for (int i = 0; i < a.GetLength(0); i++)
- for (int j = 0; j < a.GetLength(1); j++)
- if (a[i, j] > a[imA, jmA]) { imA = i; jmA = j; }
- for (int i = 0; i < b.GetLength(0); i++)
- for (int j = 0; j < b.GetLength(1); j++)
- if (b[i, j] > b[imB, jmB]) { imB = i; jmB = j; }
- SwapMax(a, b);
- Console.WriteLine("Результирующая A:"); Output(a);
- Console.WriteLine("Результирующая B:"); Output(b);
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д