Другие языки программирования и технологии

с# Дано 2 двумерных массива. Переставить столбцы с максимальными и минимальными элементами

Почти все есть. не могу только переставить столбцы. Как правильно. запуталась в индексах
http://pastebin.com/gU6DVZre
using System;
namespace example {
  class Program {
    static void Main() {
      int[][] mx33 = Matrix(3);
      Show(mx33);
      MaxMinColumnElements(mx33);
      Show(mx33);
      int[][] mx44 = Matrix(4);
      Show(mx44);
      MaxMinColumnElements(mx44);
      Show(mx44);
      Console.ReadKey();
    }
    static void Show(int[][] mx) {
      for (int r = 0; r < mx.GetLength(0); ++r, Console.WriteLine()) for (int c = 0; c < mx[r].GetLength(0); ++c) Console.Write("{0,5}", mx[r][c]);  
      Console.WriteLine();
    }
    static int[][] Matrix(uint size) {
      Random rand = new Random();
      int[][] mx = new int[size][];
      for (int r = 0; r < size; ++r) {
        mx[r] = new int[size];
        for (int c = 0; c < size; ++c) mx[r][c] = rand.Next(-9, 10);
      }
      return mx;
    }
    static void MaxMinColumnElements(int[][] mx) {
      int max = MaxIndex(mx), min = MinIndex(mx);
      if (max == min) max = MaxIndexRev(mx);
      else if (max == min) min = MinIndexRev(mx);
      else if (max != min) for (int r = 0; r < mx.GetLength(0); ++r) Swap(ref mx[r][max], ref mx[r][min]);
      else Console.WriteLine(" Максимальное и минимальное значение расположены в одной колонке.");
    }
    static int MaxIndex(int[][] mx) {
      int maxIndex = new int(), max = int.MinValue;
      for (int c = 0; c < mx.GetLength(0); ++c) for (int r = 0; r < mx[c].GetLength(0); ++r) if (max < mx[r][c]) { maxIndex = c; max = mx[r][c]; }
      return maxIndex;
    }
    static int MaxIndexRev(int[][] mx) {
      int maxIndex = new int(), max = int.MinValue;
      for (int c = mx.GetLength(0) - 1; c >= 0; --c) for (int r = 0; r < mx[c].GetLength(0); ++r) if (max < mx[r][c]) { maxIndex = c; max = mx[r][c]; }
      return maxIndex;
    }
    static int MinIndex(int[][] mx) {
      int minIndex = new int(), min = int.MaxValue;
      for (int c = 0; c < mx.GetLength(0); ++c) for (int r = 0; r < mx[c].GetLength(0); ++r) if (min > mx[r][c]) { minIndex = c; min = mx[r][c]; }
      return minIndex;
    }
    static int MinIndexRev(int[][] mx) {
      int minIndex = new int(), min = int.MaxValue;
      for (int c = mx.GetLength(0) - 1; c >= 0; --c) for (int r = 0; r < mx[c].GetLength(0); ++r) if (min > mx[r][c]) { minIndex = c; min = mx[r][c]; }
      return minIndex;
    }
    static void Swap(ref int a, ref int b) { int temp = a; a = b; b = temp; }
  }
}
Иван Кузьмин
Иван Кузьмин
64 224
Лучший ответ

Похожие вопросы