stefan1919

matrixMultiplication

Sep 20th, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 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 ConsoleApplication6
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int[,] matrixFirst = {
  14. {1, 2},
  15. {3, 4},
  16. {5, 6},
  17. {7, 8}
  18. };
  19. int[,] matrixSecond = {
  20. {1, 2, 3},
  21. {4, 5, 6}
  22. };
  23. int firstRow = matrixFirst.GetLength(0);
  24. int firstCol = matrixFirst.GetLength(1);
  25. int secondRow = matrixSecond.GetLength(0);
  26. int secondCol = matrixSecond.GetLength(1);
  27. int[,] matrixResult = new int[firstRow, secondCol];
  28. for (int rows = 0; rows < firstRow; rows++)
  29. {
  30. for (int cols = 0; cols < secondCol; cols++)
  31. {
  32. int sum = 0;
  33. for (int i = 0; i < firstCol; i++)
  34. {
  35. sum += matrixFirst[rows, i] * matrixSecond[i, cols];
  36. }
  37. matrixResult[rows, cols] = sum;
  38. }
  39. }
  40. for (int i = 0; i < matrixResult.GetLength(0); i++)
  41. {
  42. for (int j = 0; j < matrixFirst.GetLength(1); j++)
  43. {
  44. Console.Write(matrixResult[i, j] + " ");
  45. }
  46. Console.WriteLine();
  47. }
  48.  
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment