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