Advertisement
SvetoslavUzunov

Matrix multiplication

Mar 22nd, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Demo
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             //variant1
  12.             var matrixA = new int[,]
  13.             {
  14.                 { 1, 2, 3 },
  15.                 { 4, 5, 6 },
  16.                 { 7, 8, 9 }
  17.             };
  18.             var matrixB = new int[,]
  19.             {
  20.                 { 0, 1, 0 },
  21.                 { 7, 3, 4 },
  22.                 { 2, 6, 3 }
  23.             };
  24.  
  25.             var matrixC = new int[3, 3];
  26.             int sum = 0;
  27.             for (int row = 0; row < matrixA.GetLength(0); row++)
  28.             {
  29.                 for (int col = 0; col < matrixB.GetLength(1); col++)
  30.                 {
  31.                     for (int k = 1; k <= 3; k++)
  32.                     {
  33.                         sum += matrixA[row, k - 1] * matrixB[k - 1, col];
  34.                     }
  35.                     matrixC[row, col] = sum;
  36.                     sum = 0;
  37.                 }
  38.             }
  39.  
  40.             for (int i = 0; i < matrixC.GetLength(0); i++)
  41.             {
  42.                 for (int j = 0; j < matrixC.GetLength(1); j++)
  43.                 {
  44.                     Console.Write(matrixC[i, j] + " ");
  45.                 }
  46.                 Console.WriteLine();
  47.             }
  48.  
  49.             //Variant2
  50.             var matrixA = new int[,]
  51.             {
  52.                 { 1, 2, 3 },
  53.                 { 4, 5, 6 },
  54.                 { 7, 8, 9 }
  55.             };
  56.             var matrixB = new int[,]
  57.             {
  58.                 { 0, 1, 0 },
  59.                 { 7, 3, 4 },
  60.                 { 2, 6, 3 }
  61.             };
  62.  
  63.             var matrixC = new int[3, 3];
  64.             for (int row = 0; row < matrixA.GetLength(0); row++)
  65.             {
  66.                 for (int col = 0; col < matrixB.GetLength(1); col++)
  67.                 {
  68.                     matrixC[row, col] = 0;
  69.                     for (int k = 0; k < 3; k++)
  70.                     {
  71.                         matrixC[row, col] += matrixA[row, k] * matrixB[k, col];
  72.                     }
  73.                 }
  74.             }
  75.  
  76.             for (int row = 0; row < matrixC.GetLength(0); row++)
  77.             {
  78.                 for (int col = 0; col < matrixC.GetLength(1); col++)
  79.                 {
  80.                     Console.Write(matrixC[row, col] + " ");
  81.                 }
  82.                 Console.WriteLine();
  83.             }
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement