Advertisement
sergezhu

Untitled

Apr 30th, 2023
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. namespace ConsoleApp1;
  2.  
  3. using System.Text;
  4.  
  5. public class Task21
  6. {
  7.     public void Run()
  8.     {
  9.         Console.InputEncoding = Encoding.Unicode;
  10.         Console.OutputEncoding = Encoding.Unicode;
  11.  
  12.         int randomRangeIncludedLowBound = 1;
  13.         int randomRangeExcludedHighBound = 10;
  14.         Random random = new Random();
  15.        
  16.         int columnsCount = 10;
  17.         int rowsCount = 10;
  18.         int[,] matrix = new int[rowsCount, columnsCount];
  19.  
  20.         int maxElementChangedValue = 0;
  21.        
  22.         bool canExit = false;
  23.  
  24.         while ( canExit == false )
  25.         {
  26.             Console.Clear();
  27.  
  28.             int maxElement = int.MinValue;
  29.             string displayedMatrix = string.Empty;
  30.  
  31.             for ( int i = 0; i < rowsCount; i++ )
  32.             {
  33.                 for ( int j = 0; j < columnsCount; j++ )
  34.                 {
  35.                     matrix[i, j] = random.Next( randomRangeIncludedLowBound, randomRangeExcludedHighBound );
  36.                     displayedMatrix = $"{displayedMatrix}{matrix[i, j]} ";
  37.  
  38.                     if ( matrix[i, j] > maxElement )
  39.                         maxElement = matrix[i, j];
  40.                 }
  41.                
  42.                 displayedMatrix = $"{displayedMatrix}\n";
  43.             }
  44.  
  45.             displayedMatrix = $"{displayedMatrix}\n";
  46.            
  47.             Console.WriteLine("Source matrix:");
  48.             Console.WriteLine( displayedMatrix );
  49.            
  50.  
  51.             displayedMatrix = string.Empty;
  52.  
  53.             for ( int i = 0; i < rowsCount; i++ )
  54.             {
  55.                 for ( int j = 0; j < columnsCount; j++ )
  56.                 {
  57.                     if ( matrix[i, j] == maxElement )
  58.                         matrix[i, j] = maxElementChangedValue;
  59.  
  60.                     displayedMatrix = $"{displayedMatrix}{matrix[i, j]} ";
  61.                 }
  62.  
  63.                 displayedMatrix = $"{displayedMatrix}\n";
  64.             }
  65.  
  66.             displayedMatrix = $"{displayedMatrix}\n";
  67.            
  68.             Console.WriteLine( $"Matrix after swap of max element ({maxElement}) to {maxElementChangedValue}:" );
  69.             Console.WriteLine( displayedMatrix );
  70.            
  71.  
  72.             string properlyExitAnswer = "n";
  73.             Console.WriteLine( $"Continue? Enter \'{properlyExitAnswer}\' for exit" );
  74.             string continueAnswer = Console.ReadLine();
  75.  
  76.             canExit = string.Equals( continueAnswer, properlyExitAnswer );
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement