TheBulgarianWolf

Random matrix + Transposed Matrix

Apr 16th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace OnlineExercise15._4
  8. {
  9.     class Program
  10.     {
  11.         static Random randomGenerator = new Random();
  12.  
  13.  
  14.         static void GenerateMatrix(int[,] matrix)
  15.         {
  16.             for (int i = 0; i < matrix.GetLength(0); i++)
  17.             {
  18.                 for (int j = 0; j < matrix.GetLength(1); j++)
  19.                 {
  20.                     matrix[i, j] = randomGenerator.Next(-100, 100);
  21.                 }
  22.             }
  23.         }
  24.  
  25.         static void PrintMatrix(int[,] matrix)
  26.         {
  27.             for (int i = 0; i < matrix.GetLength(0); i++)
  28.             {
  29.                 for (int j = 0; j < matrix.GetLength(1); j++)
  30.                 {
  31.                     Console.Write(matrix[i, j] + " ");
  32.                 }
  33.                 Console.WriteLine();
  34.             }
  35.         }
  36.  
  37.         static int[,] Transpose(int[,] matrix)
  38.         {
  39.             int w = matrix.GetLength(0);
  40.             int h = matrix.GetLength(1);
  41.  
  42.             int[,] result = new int[h,w];
  43.             for(int i = 0; i < w; i++)
  44.             {
  45.                 for(int j = 0; j < h; j++)
  46.                 {
  47.                     result[j, i] = matrix[i, j];
  48.                 }
  49.             }
  50.             return result;
  51.         }
  52.  
  53.         static void PrintTransposed(int[,] matrix)
  54.         {
  55.             for (int i = 0; i < matrix.GetLength(0); i++)
  56.             {
  57.                 for (int j = 0; j < matrix.GetLength(1); j++)
  58.                 {
  59.                     Console.Write(matrix[i, j] + " ");
  60.                 }
  61.                 Console.WriteLine();
  62.             }
  63.         }
  64.  
  65.         static void Main(string[] args)
  66.         {
  67.            
  68.  
  69.             int[,] matrix = new int[5, 4];
  70.             GenerateMatrix(matrix);
  71.             PrintMatrix(matrix);
  72.             Console.WriteLine();
  73.             Console.WriteLine();
  74.             PrintTransposed(Transpose(matrix));
  75.             Console.ReadKey();
  76.        }
  77.  
  78.  
  79.     }
  80. }
Add Comment
Please, Sign In to add comment