fork(1) download
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. int main() {
  4.  
  5. int arrMatrixMultiplication(int *matrix1, int firstRow, int firstColumn, int *matrix2, int secondRow, int secondColumn, int *resultMatrix);
  6. void arrFillRandom(int *arrayPtr, int sizeRow, int sizeColumn, int randomRange);
  7.  
  8. int matrix1[2][3];
  9. int matrix2[3][2];
  10.  
  11. int *ptr1, *ptr2;
  12. ptr1 = &matrix1[0][0];
  13. ptr2 = &matrix2[0][0];
  14.  
  15. arrFillRandom(ptr1, 2, 3, 10);
  16. arrFillRandom(ptr2, 3, 2, 10);
  17.  
  18. int resultMatrix[2][2];
  19. int *ptr3;
  20. ptr3 = &resultMatrix[0][0];
  21.  
  22. arrMatrixMultiplication(ptr1, 2, 3, ptr2, 3, 2, ptr3);
  23. printf("\nThe result is: \n");
  24. printf("%d", ptr3[0]);
  25.  
  26. return 0;
  27. }
  28.  
  29. int arrMatrixMultiplication(int *matrix1, int firstRow, int firstColumn, int *matrix2, int secondRow, int secondColumn, int *resultMatrix) {
  30. int i, j, k, sum = 0;
  31.  
  32. if (firstColumn != secondRow) {
  33. return 0;
  34. } else {
  35. for (i = 0; i < firstRow; i++) {
  36. for (j = 0; j < secondColumn; j++) {
  37. for (k = 0; k < secondRow; k++) {
  38. sum = sum + (matrix1[i * secondColumn + k] * matrix2[k * secondColumn + j]);
  39. }
  40. resultMatrix[i * secondColumn + j] = sum;
  41. sum = 0;
  42. }
  43. }
  44. }
  45.  
  46. for (i = 0; i < firstRow; i++) {
  47. printf("\n");
  48. for (j = 0; j < secondColumn; j++)
  49. printf("%d\t", resultMatrix[i * secondColumn + j]);
  50. }
  51. }
  52.  
  53. void arrFillRandom(int *arrayPtr, int sizeRow, int sizeColumn, int randomRange) {
  54. int i, j;
  55.  
  56. for (i = 0; i < sizeRow; i++)
  57. for (j = 0; j < sizeColumn; j++)
  58. (arrayPtr + i)[j] = (rand() % randomRange);
  59.  
  60. for (i = 0; i < sizeRow; i++) {
  61. printf("\n");
  62. for (j = 0; j < sizeColumn; j++)
  63. printf("%d\t", (arrayPtr + i)[j]);
  64. }
  65. }
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
3	5	3	
5	3	5	
6	9	
9	2	
2	7	
403543789	644927514	
134514615	-1216679886	
The result is: 
403543789