1double[][] c = new double[N][N];
2for (int i = 0; i < N; i++)
3{
4 for (int j = 0; j < N; j++)
5 {
6 for (int k = 0; k < N; k++)
7 {
8 c[i][j] += a[i][k] * b[k][j];
9 }
10 }
11}
12
1#include <stdio.h>
2
3// function to get matrix elements entered by the user
4void getMatrixElements(int matrix[][10], int row, int column) {
5
6 printf("\nEnter elements: \n");
7
8 for (int i = 0; i < row; ++i) {
9 for (int j = 0; j < column; ++j) {
10 printf("Enter a%d%d: ", i + 1, j + 1);
11 scanf("%d", &matrix[i][j]);
12 }
13 }
14}
15
16// function to multiply two matrices
17void multiplyMatrices(int first[][10],
18 int second[][10],
19 int result[][10],
20 int r1, int c1, int r2, int c2) {
21
22 // Initializing elements of matrix mult to 0.
23 for (int i = 0; i < r1; ++i) {
24 for (int j = 0; j < c2; ++j) {
25 result[i][j] = 0;
26 }
27 }
28
29 // Multiplying first and second matrices and storing it in result
30 for (int i = 0; i < r1; ++i) {
31 for (int j = 0; j < c2; ++j) {
32 for (int k = 0; k < c1; ++k) {
33 result[i][j] += first[i][k] * second[k][j];
34 }
35 }
36 }
37}
38
39// function to display the matrix
40void display(int result[][10], int row, int column) {
41
42 printf("\nOutput Matrix:\n");
43 for (int i = 0; i < row; ++i) {
44 for (int j = 0; j < column; ++j) {
45 printf("%d ", result[i][j]);
46 if (j == column - 1)
47 printf("\n");
48 }
49 }
50}
51
52int main() {
53 int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2;
54 printf("Enter rows and column for the first matrix: ");
55 scanf("%d %d", &r1, &c1);
56 printf("Enter rows and column for the second matrix: ");
57 scanf("%d %d", &r2, &c2);
58
59 // Taking input until
60 // 1st matrix columns is not equal to 2nd matrix row
61 while (c1 != r2) {
62 printf("Error! Enter rows and columns again.\n");
63 printf("Enter rows and columns for the first matrix: ");
64 scanf("%d%d", &r1, &c1);
65 printf("Enter rows and columns for the second matrix: ");
66 scanf("%d%d", &r2, &c2);
67 }
68
69 // get elements of the first matrix
70 getMatrixElements(first, r1, c1);
71
72 // get elements of the second matrix
73 getMatrixElements(second, r2, c2);
74
75 // multiply two matrices.
76 multiplyMatrices(first, second, result, r1, c1, r2, c2);
77
78 // display the result
79 display(result, r1, c2);
80
81 return 0;
82}
1import java.util.Arrays;
2import java.util.Scanner;
3public class Matrix {
4
5 private static int counter =0;
6 private static Scanner scanner = new Scanner(System.in);
7 private int rawForArr1 =0 , columnForArr1=0, rawForArr2=0, ColumnForArr2=0;
8
9 public Matrix(){
10 }
11
12 public Matrix(int rawForArr1, int columnForArr1, int rawForArr2, int columnForArr2) {
13 this.rawForArr1 = rawForArr1;
14 this.columnForArr1 = columnForArr1;
15 this.rawForArr2 = rawForArr2;
16 ColumnForArr2 = columnForArr2;
17 }
18
19 public void sizeOfRawsAndColumns(int r1, int c1, int r2, int c2){
20 System.out.println("Enter numbers of raws for array#1");
21 r1=scanner.nextInt();
22 this.rawForArr1 =r1;
23 System.out.println("Enter numbers of columns for array#1");
24 c1=scanner.nextInt();
25 this.columnForArr1=c1;
26 System.out.println("Enter numbers of raws for array#2");
27 r2=scanner.nextInt();
28 this.rawForArr2 = r2;
29 System.out.println("Enter numbers of columns for array#2");
30 c2=scanner.nextInt();
31 this.ColumnForArr2 = c2;
32 checkIfMatrixIsValid(c1,r2,r1,c2);
33 }
34 public static void printValue(int[][] arr)
35 {
36 System.out.print(Arrays.deepToString(arr));
37 }
38
39 public static void checkIfMatrixIsValid(int c1,int r2,int r1, int c2)
40 {
41 while(c1 != r2)
42 {
43 System.out.println("The columns in the left array is not equal to the raw's in the right"+"\n"+
44 "array enter other values"+"\n"+
45 "enter a value and it will be assigned for valid Matrix");
46 Scanner scanner = new Scanner(System.in);
47 c1 = scanner.nextInt();
48 r2 = c1;
49 }
50 int [][] firstArr = initilizeMatrix(r1,c1);
51 int [][] secondArr = initilizeMatrix(r2,c2);
52 EnterElements(firstArr);
53 EnterElements(secondArr);
54 int[][] multplied =multiplieMatrix(firstArr,secondArr);
55 printValue(multplied);
56
57
58
59 }
60
61 public static int [][] initilizeMatrix(int n , int m)
62 {
63
64 int [][] newMatrix = new int[m][n];
65 return newMatrix;
66
67 }
68
69 public static int [][] multiplieMatrix(int [][] leftArr,int[][] rightArr){
70 int m = leftArr[0].length;
71 int n = rightArr.length;
72 int [][] sumOfMultiplie = new int[m][n];
73 int i=0,j=0,k=0;
74 int sum = 0 ;
75 for( i = 0 ; i < leftArr.length ; ++i)
76 {
77 for(j = 0 ; j < rightArr.length ; ++j)
78 {
79 for(k = 0 ; k < rightArr[0].length ; ++k)
80 {
81 sum+= leftArr[i][k]*rightArr[k][j];
82 }
83 sumOfMultiplie[i][j]=sum ;
84 sum=0;
85 }
86 }
87 return sumOfMultiplie ;
88 }
89
90 public static int[][] EnterElements(int[][] arr)
91 {
92
93 ++counter;
94 if(counter==1){
95 System.out.println("Enter the elements for the left array"+'\n');
96 }else{
97 System.out.println("Enter the elements for the right array");
98 }
99
100 for(int i = 0 ; i < arr.length ; ++i)
101 {
102 for(int j = 0 ; j<arr.length ; ++j)
103 {
104 System.out.println("Enter element in raw #"+(i+1)+" column #"+(j+1));
105 arr[i][j] = scanner.nextInt();
106 }
107 }
108 return arr ;
109
110 }
111
112
113
114}