matrix.pp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. { Matrix Multiplication }
  2. program matrix;
  3. uses SysUtils;
  4. const
  5. size = 30;
  6. type tMatrix = array[0..size, 0..size] of longint;
  7. procedure mkmatrix( rows, cols : integer; var mx : tMatrix);
  8. var
  9. R, C : integer;
  10. count : longint;
  11. begin
  12. Dec(rows);
  13. Dec(cols);
  14. count := 1;
  15. for R := 0 to rows do
  16. begin
  17. for C := 0 to cols do
  18. begin
  19. mx[R, C] := count;
  20. Inc(count);
  21. end;
  22. end;
  23. End;
  24. procedure mmult(rows, cols : integer; m1, m2 : tMatrix; var mm : tMatrix );
  25. var
  26. i, j, k : integer;
  27. val: longint;
  28. begin
  29. Dec(rows);
  30. Dec(cols);
  31. For i := 0 To rows do
  32. begin
  33. For j := 0 To cols do
  34. begin
  35. val := 0;
  36. For k := 0 To cols do
  37. begin
  38. Inc(val, m1[i, k] * m2[k, j]);
  39. end;
  40. mm[i, j] := val;
  41. end;
  42. end;
  43. End;
  44. var NUM, I : integer;
  45. M1, M2, MM : tMatrix;
  46. begin
  47. if ParamCount = 0 then
  48. NUM := 1
  49. else
  50. NUM := StrToInt(ParamStr(1));
  51. if NUM < 1 then NUM := 1;
  52. mkmatrix(size, size, M1);
  53. mkmatrix(size, size, M2);
  54. for I := 0 To NUM do
  55. begin
  56. mmult(size, size, M1, M2, MM);
  57. end;
  58. WriteLn( IntToStr(MM[0, 0]) + ' ' + IntToStr(MM[2, 3]) + ' ' +
  59. IntToStr(MM[3, 2]) + ' ' + IntToStr(MM[4, 4]));
  60. end.