Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MatrixSoftuni
- {
- static class MatrixMath
- {
- public static void PrintMatrix(int[,] matrix)
- {
- int n = matrix.GetLength(0);
- int m = matrix.GetLength(1);
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- Console.Write($"{matrix[i, j]} ");
- }
- Console.WriteLine();
- }
- }
- public static void RandomElements(int[,] matrix)
- {
- Random rand = new Random();
- int n = matrix.GetLength(0);
- int m = matrix.GetLength(1);
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- matrix[i, j] = rand.Next(10);
- }
- }
- }
- public static void ChangeRows(int[,] matrix, int row1, int row2)
- {
- int n = matrix.GetLength(0);
- int m = matrix.GetLength(1);
- if (row1 < 0 || row2 < 0 || row1 >= n || row2 >= n)
- {
- throw new ArgumentException("Rows problem");
- }
- for (int i = 0; i < m; i++)
- {
- // matrix[row1, i] <=> matrix[row2, i]
- int temp = matrix[row1, i];
- matrix[row1, i] = matrix[row2, i];
- matrix[row2, i] = temp;
- }
- }
- public static int[,] ScalarMult(int[,] matrix, int num)
- {
- int n = matrix.GetLength(0);
- int m = matrix.GetLength(1);
- int[,] result = new int[n, m];
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- result[i, j] = matrix[i, j] * num;
- }
- }
- return result;
- }
- public static int[,] AddMatrix(int[,] matrix1, int[,] matrix2)
- {
- int n1 = matrix1.GetLength(0);
- int m1 = matrix1.GetLength(1);
- int n2 = matrix1.GetLength(0);
- int m2 = matrix1.GetLength(1);
- if (n1 != n2 || m1 != m2)
- {
- throw new ArgumentException("Matrix dimensions problem!");
- }
- int[,] result = new int[n1, m1];
- for (int i = 0; i < n1; i++)
- {
- for (int j = 0; j < m1; j++)
- {
- result[i, j] = matrix1[i, j] + matrix2[i, j];
- }
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement