RMarK0

Димаса задание матрицы

May 16th, 2020
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp1
  4. {
  5.     class Program
  6.     {
  7.         static void Compare(int[,] array1, int[,] array2)
  8.         {
  9.             int counter1 = 0;
  10.             for (int i = 0; i < array1.GetLength(dimension: 0); i++)
  11.             {
  12.                 for (int j = 0; j < array1.GetLength(dimension: 1); j++)
  13.                 {
  14.                     if (array1[i,j] < 0)
  15.                         counter1++;
  16.                 }
  17.             }
  18.  
  19.             int counter2 = 0;
  20.             for (int i = 0; i < array2.GetLength(dimension: 0); i++)
  21.             {
  22.                 for (int j = 0; j < array2.GetLength(dimension: 1); j++)
  23.                 {
  24.                     if (array2[i,j] < 0)
  25.                         counter2++;
  26.                 }
  27.             }
  28.  
  29.             Console.ForegroundColor = ConsoleColor.Green;
  30.             if (counter1 > counter2)
  31.             {
  32.                 Console.WriteLine("Matrix 1 = {0}, Matrix 2 = {1}", counter1, counter2);
  33.                 Console.WriteLine("В матрице 2 меньше отриц. чисел, чем в матрице 1\n");
  34.                 DeleteStroke(array2);
  35.             }
  36.             else if (counter1 < counter2)
  37.             {
  38.                 Console.WriteLine("Matrix 1 = {0}, Matrix 2 = {1}", counter1, counter2);
  39.                 Console.WriteLine("В матрице 1 меньше отриц. чисел, чем в матрице 2\n");
  40.                 DeleteStroke(array1);
  41.             }
  42.             else
  43.             {
  44.                 Console.WriteLine("У матриц одинаковое кол-во отрицательных чисел");
  45.                 throw new NotImplementedException("Матрицы одинаковые");
  46.             }
  47.         }
  48.  
  49.         static void DeleteStroke(int[,] array)
  50.         {
  51.             Console.ResetColor();
  52.             int Max = Int32.MinValue;
  53.             int MaxIndex = -1;
  54.  
  55.             for (int i = 0; i < array.GetLength(0); i++)
  56.             {
  57.                 int StrokeSum = 0;
  58.                 for (int j = 0; j < array.GetLength(1); j++)
  59.                 {
  60.                     StrokeSum += array[i, j];
  61.                 }
  62.                 Console.WriteLine("Сумма элементов строки {0} = {1}",i+1,StrokeSum);
  63.  
  64.                 if (StrokeSum > Max)
  65.                 {
  66.                     Max = StrokeSum;
  67.                     MaxIndex = i;
  68.                 }
  69.             }
  70.             Console.WriteLine();
  71.             for (int j = 0; j < array.GetLength(1); j++)
  72.                 array[MaxIndex, j] = -100;
  73.  
  74.             for (int i = 0; i < array.GetLength(dimension: 0); i++)
  75.             {
  76.                 for (int j = 0; j < array.GetLength(dimension: 1); j++)
  77.                 {
  78.                     if (array[i,j] != -100)
  79.                         Console.Write("{0,3}", array[i, j]);
  80.                 }
  81.                 Console.WriteLine();
  82.             }
  83.         }
  84.        
  85.         static void CreateMatrix(ref int[,] a, Random r)
  86.         {
  87.            
  88.             Console.WriteLine("\nМатрица");
  89.             for (int i = 0; i < a.GetLength(dimension: 0); i++)
  90.             {
  91.                 for (int j = 0; j < a.GetLength(dimension: 1); j++)
  92.                 {
  93.                     a[i, j] = r.Next(-5, 6);
  94.                     Console.Write("{0,3}", a[i, j]);
  95.                 }
  96.                 Console.WriteLine();
  97.             }
  98.         }
  99.         static void Main(string[] args)
  100.         {
  101.             int[,] Array1 = new int[3,3];
  102.             int[,] Array2 = new int[4,3];
  103.  
  104.             Random r = new Random(9951967);
  105.             CreateMatrix(ref Array1, r);
  106.             CreateMatrix(ref Array2, r);
  107.  
  108.             Console.WriteLine("Press any key to continue...");
  109.             Console.ReadLine();
  110.  
  111.             Compare(Array1, Array2);
  112.             Console.Read();
  113.         }
  114.     }
  115. }
Add Comment
Please, Sign In to add comment