Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Macierze
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. int[,] tabA = new int[3, 3] { { 1, 3, 2 }, { -2, 0, 1 }, { 5, -3, 2 } };
  10. int[,] tabB = new int[2, 3] { { 2, 1, 3 }, { -1, 4, 0 } };
  11.  
  12. int[,] result = Multiply(tabA, tabB);
  13.  
  14. Draw(result);
  15.  
  16. Console.ReadKey();
  17. }
  18.  
  19. public static int[,] Multiply (int[,] tabA, int[,] tabB)
  20. {
  21. if (tabA.GetLength(1) == tabB.GetLength(1))
  22. {
  23. int[,] result;
  24. int row = new int();
  25. bool aFirst = true;
  26.  
  27. if (tabA.Length > tabB.Length)
  28. {
  29. result = new int[tabB.GetLength(0), tabA.GetLength(1)];
  30. aFirst = false;
  31. }
  32. else
  33. {
  34. result = new int[tabA.GetLength(0), tabB.GetLength(1)];
  35. aFirst = false;
  36. }
  37.  
  38.  
  39.  
  40. for (int i = 0; i < tabB.GetLength(0); i++)
  41. {
  42. for (int j = 0; j < tabA.GetLength(0); j++)
  43. {
  44. for (int k = 0; k < tabB.GetLength(1); k++)
  45. {
  46. try
  47. {
  48. if (aFirst)
  49. row += tabA[i, k] * tabB[k, j];
  50. else
  51. row += tabB[i, k] * tabA[k, j];
  52. }
  53. catch (IndexOutOfRangeException ex)
  54. {
  55. Console.WriteLine(ex.Message + " (k - " + k + " j - " + j + " i - " + i + ") aFirst = " + aFirst);
  56. }
  57. }
  58. result[i, j] = row;
  59. row = 0;
  60. }
  61. }
  62. return result;
  63. }
  64. else
  65. return new int[0,0];
  66.  
  67.  
  68. }
  69.  
  70. public static void Draw(int[,] macierz)
  71. {
  72. for (int i = 0; i < macierz.GetLength(0); i++)
  73. {
  74. for (int j = 0; j < macierz.GetLength(1); j++)
  75. {
  76. Console.Write(macierz[i, j] + " ");
  77. }
  78. Console.WriteLine();
  79. }
  80. }
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement