Matrix multiplication by two-dimensional array using java programming


Steps:

1.      Define three nxn matrices

2.      Take input for matrix A

3.      Take input for matrix B

4.      Multiply matrix AxB, and put the result in matrix C

5.      Output matrix C

6.      Check you result by an online matrix multiplication calculator

Pseudo code:

int i, j, k, n;

int A[n][n], B[n][n], C[n][n];

n=3;

//input for A

For (i=0; i<n; i++)

{

      For(j=0; j<n; j++)

      {      

Take input for A[i][j];

      }

}

 

//input for B

For (i=0; i<n; i++)

{

     For(j=0; j<n; j++)

      {      

Take input for B[i][j];

      }

}

 

// Multiply C = AxB

For(i=0; i<n; i++)

{

     For(j=0; j<n; j++)

     {

               C[i][j] = 0;

               For(k=0; k<n; k++)  

                     C[i][j] += A[i][k]*B[k][j];

      }

}

 

 

// Output C

For (i=0; i<n; i++)

{

     For(j=0; j<n; j++)

      {      

               Print C[i][j];

      }

}

الأجوبة

ابحث عن مسائل برمجة جافا | Java programming بالانجليزي

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("");
		}
	}
}

محتاج مساعدة؟ تواصل مع مدرس اونلاين الان!