RMarK0

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

Apr 25th, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.25 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4.  
  5. // n*m
  6. // Удалить строки содерж макс элементы матрицы
  7. // использовать метод
  8. namespace ConsoleApp1
  9. {
  10.     class Program
  11.     {
  12.         public static void FindAndDelete(ref int[,] a)
  13.         {
  14.             int max = Int32.MinValue;
  15.             for (int j = 0; j < a.GetLength(dimension: 1); j++)
  16.             {
  17.                 for (int i = 0; i < a.GetLength(dimension: 0); i++)
  18.                 {
  19.                     if (a[i, j] > max)
  20.                         max = a[i, j];
  21.                 }
  22.             }
  23.  
  24.             for (int i = 0; i < a.GetLength(dimension: 0); i++)
  25.             {
  26.                 bool FoundMax = false;
  27.                 for (int j = 0; j < a.GetLength(dimension: 1); j++)
  28.                 {
  29.                     if (a[i, j] == max)
  30.                         FoundMax = true;
  31.                 }
  32.  
  33.                 if (!FoundMax)
  34.                 {
  35.                     for (int jj = 0; jj < a.GetLength(dimension: 1); jj++)
  36.                     {
  37.                         Console.Write("{0,6}", a[i,jj]);
  38.                     }
  39.                     Console.WriteLine();
  40.                 }
  41.                 else
  42.                 {
  43.                     Console.WriteLine("Строка удалена");
  44.                 }
  45.                 FoundMax = false;
  46.             }
  47.  
  48.         }
  49.  
  50.         static void CreateMatrix(ref int[,] a)
  51.         {
  52.             Random r = new Random(1231471366);
  53.             Console.WriteLine("Матрица");
  54.             for (int i = 0; i < a.GetLength(dimension: 0); i++)
  55.             {
  56.                 for (int j = 0; j < a.GetLength(dimension: 1); j++)
  57.                 {
  58.                     a[i, j] = r.Next(-2, 10);
  59.                     Console.Write("{0, 6}", a[i, j]);
  60.                 }
  61.                 Console.WriteLine();
  62.             }
  63.         }
  64.  
  65.         static void Main(string[] args)
  66.         {
  67.             const int n = 4;
  68.             const int m = 5;
  69.             int[,] array = new int[n, m];
  70.             CreateMatrix(ref array);
  71.             Console.WriteLine();
  72.             Console.ForegroundColor = ConsoleColor.Green;
  73.             FindAndDelete(ref array);
  74.  
  75.            
  76.             Console.Read();
  77.  
  78.         }
  79.     }
  80. }
Add Comment
Please, Sign In to add comment