Matrix multiplication by two-dimensional array using java programming
- برمجة جافا
- برمجة
- 2021-09-18
- softwareEngineer
الأجوبة
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int rows,columns;
System.out.println("enter the number of rows and columns:");
rows=columns=input.nextInt();
int [][]A=new int[rows][columns];
int [][]B=new int[rows][columns];
int [][]C=new int[rows][columns];
//input values of A
System.out.println("please enter the values of array A:");
for(int row=0;row<rows;row++)
{
for(int column=0;column<columns;column++)
{
System.out.println("enter the value of A["+row+"]"+"["+column+"] : ");
A[row][column]=input.nextInt();
}
}
//input values of B
System.out.println("please enter the values of array B:");
for(int r=0;r<rows;r++)
{
for(int c=0;c<columns;c++)
{
System.out.println("enter the value of B["+r+"]"+"["+c+"] : ");
B[r][c]=input.nextInt();
}
}
// Multiply C = AxB
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
C[i][j] = 0;
for(int k=0; k<rows; k++)
C[i][j] += A[i][k]*B[k][j];
}
}
System.out.println("this is the array A:");
for(int n=0;n<rows;n++)
{
for(int m=0;m<columns;m++)
{
System.out.print(A[n][m]+" ");
}
System.out.println("");
}
System.out.println("this is the array B:");
for(int n=0;n<rows;n++)
{
for(int m=0;m<columns;m++)
{
System.out.print(B[n][m]+" ");
}
System.out.println("");
}
System.out.println("this is the resulting array C:");
for(int n=0;n<rows;n++)
{
for(int m=0;m<columns;m++)
{
System.out.print(C[n][m]+" ");
}
System.out.println("");
}
}
}
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال