fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5.  
  6.  
  7.  
  8. public static void MultiplyMatrix()
  9. {
  10. int[,] a = new int[5, 5];
  11. int[,] b = new int[5, 5];
  12. int[,] c = new int[5, 5];
  13.  
  14. Random ran = new Random();
  15.  
  16. // Инициализируем данный массив
  17. for (int i = 0; i < 4; i++)
  18. {
  19. for (int j = 0; j < 5; j++)
  20. {
  21. a[i, j] = ran.Next(1, 15);
  22. b[i, j] = ran.Next(1, 15);
  23. c[i, j] = ran.Next(1, 15);
  24. }
  25. Console.WriteLine();
  26. }
  27.  
  28. if (a.GetLength(1) == b.GetLength(0))
  29. {
  30. c = new int[a.GetLength(0), b.GetLength(1)];
  31. for (int i = 0; i < c.GetLength(0); i++)
  32. {
  33. for (int j = 0; j < c.GetLength(1); j++)
  34. {
  35. c[i, j] = 0;
  36. for (int k = 0; k < a.GetLength(1); k++) // OR k<b.GetLength(0)
  37. c[i, j] = c[i, j] + a[i, k] * b[k, j];
  38. }
  39. }
  40. }
  41. else
  42. {
  43. Console.WriteLine(c[1, 1]);
  44. Console.WriteLine("\n Number of columns in First Matrix should be equal to Number of rows in Second Matrix.");
  45. Console.WriteLine("\n Please re-enter correct dimensions.");
  46. Environment.Exit(-1);
  47. }
  48. }
  49.  
  50. public static void Main()
  51. {
  52.  
  53. MultiplyMatrix ();
  54. }
  55. }
Success #stdin #stdout 0.04s 23888KB
stdin
Standard input is empty
stdout