1. #include <stdio.h>
2.
3. int** addMatrix(int a[3][3], int b[3][3]){
4. int i,j;
5. int **result = (int **)malloc(3 * sizeof(int *));
6. int row;
7. for (row = 0; row < 3; row++) {
8. result[row] = (int *)malloc(3* sizeof(int));
9. }
10.
11. for(i=0;i<3;i++){
12. for(j=0;j<3;j++){
13. result[i][j]=0;
14. result[i][j]=a[i][j]+b[i][j];
15. }
16. }
17. return result;
18. }
19.
20. int** mulMatrix(int a[3][3],int b[3][3]){
21. int **result = (int **)malloc(3 * sizeof(int *));
22. int row;
23. for (row = 0; row < 3; row++) {
24. result[row] = (int *)malloc(3* sizeof(int));
25. }
26.
27. int i,j,k;
28. for(i=0;i<3;i++){
29. for(j=0;j<3;j++){
30. result[i][j] = 0;
31. }
32. }
33.
34. for(i=0;i<3;i++){
35. for(j=0;j<3;j++){
36. for(k=0;k<3;k++){
37. result[i][j] += a[i][k] * b[k][j];
38. }
39. }
40. }
41. return result;
42. }
43.
44. int main(void) {
45. //Initialisng the matrices
46. int A[3][3] = {{2,-2,8},{1,3,6},{0,5,4}};
47. int B[3][3] = {{3,7,3},{5,5,6},{1,2,8}};
48. int AplusB[3][3];
49. int **partA = addMatrix(A,B); //returns A+B
50. int i=0,j=0;
52. for(i=0;i<3;i++){
53. for(j=0;j<3;j++){
54. AplusB[i][j]=partA[i][j];
55. printf("%d\t",partA[i][j]);
56. }
57. printf("\n");
58. }
59.
60. int **partB = mulMatrix(A,B); //returns AB
62. for(i=0;i<3;i++){
63. for(j=0;j<3;j++){
64. printf("%d\t",partB[i][j]);
65. }
66. printf("\n");
67. }
68.
69. int **partC = mulMatrix(AplusB,A); //return (A+B)A
71. for(i=0;i<3;i++){
72. for(j=0;j<3;j++){
73. printf("%d\t",partC[i][j]);
74. }
75. printf("\n");
76. }
77. return 0;
78. }
79.
Success #stdin #stdout 0s 4180KB
stdin
Standard input is empty
stdout