Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

2d matrix

import java.util.Arrays;
import java.util.Scanner;
public class Matrix {

    private static int counter =0;
    private static Scanner scanner = new Scanner(System.in);
    private int rawForArr1 =0 , columnForArr1=0, rawForArr2=0, ColumnForArr2=0;

    public Matrix(){
    }

    public Matrix(int rawForArr1, int columnForArr1, int rawForArr2, int columnForArr2) {
        this.rawForArr1 = rawForArr1;
        this.columnForArr1 = columnForArr1;
        this.rawForArr2 = rawForArr2;
        ColumnForArr2 = columnForArr2;
    }

    public void sizeOfRawsAndColumns(int r1, int c1, int r2, int c2){
        System.out.println("Enter numbers of raws for array#1");
        r1=scanner.nextInt();
        this.rawForArr1 =r1;
        System.out.println("Enter numbers of columns for array#1");
        c1=scanner.nextInt();
        this.columnForArr1=c1;
        System.out.println("Enter numbers of raws for array#2");
        r2=scanner.nextInt();
        this.rawForArr2 = r2;
        System.out.println("Enter numbers of columns for array#2");
        c2=scanner.nextInt();
        this.ColumnForArr2 = c2;
        checkIfMatrixIsValid(c1,r2,r1,c2);
    }
    public static void printValue(int[][] arr)
    {
                System.out.print(Arrays.deepToString(arr));
    }

    public static void checkIfMatrixIsValid(int c1,int r2,int r1, int c2)
    {
        while(c1 != r2)
        {
            System.out.println("The columns in the left array is not equal to the raw's in the right"+"
"+
                    "array enter other values"+"
"+
                    "enter a value and it will be assigned for valid Matrix");
            Scanner scanner = new Scanner(System.in);
            c1 = scanner.nextInt();
            r2 = c1;
        }
        int [][] firstArr = initilizeMatrix(r1,c1);
        int [][] secondArr = initilizeMatrix(r2,c2);
        EnterElements(firstArr);
        EnterElements(secondArr);
        int[][] multplied =multiplieMatrix(firstArr,secondArr);
        printValue(multplied);



    }

    public static int [][] initilizeMatrix(int n , int m)
    {

        int [][] newMatrix = new int[m][n];
        return  newMatrix;

    }

    public static int [][] multiplieMatrix(int [][] leftArr,int[][] rightArr){
        int m = leftArr[0].length;
        int n = rightArr.length;
        int [][] sumOfMultiplie = new int[m][n];
        int i=0,j=0,k=0;
        int sum = 0 ;
        for( i = 0 ; i < leftArr.length ; ++i)
        {
            for(j = 0 ; j < rightArr.length ; ++j)
            {
                for(k = 0 ; k < rightArr[0].length ; ++k)
                {
                    sum+= leftArr[i][k]*rightArr[k][j];
                }
                sumOfMultiplie[i][j]=sum ;
                sum=0;
            }
        }
        return sumOfMultiplie ;
    }

    public static int[][] EnterElements(int[][] arr)
    {

        ++counter;
        if(counter==1){
            System.out.println("Enter the elements for the left array"+'
');
        }else{
            System.out.println("Enter the elements for the right array");
        }

        for(int i = 0 ; i < arr.length ; ++i)
        {
            for(int j = 0 ; j<arr.length ; ++j)
            {
                System.out.println("Enter element in raw #"+(i+1)+" column #"+(j+1));
                arr[i][j] = scanner.nextInt();
            }
        }
        return arr ;

    }



}
Comment

PREVIOUS NEXT
Code Example
Java :: Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, : java.util.zip.ZipException: invalid code -- missing end-of-block 
Java :: how to get index of while loop java 
Java :: convert from java to kotlin online 
Java :: print java object inherited classes 
Java :: change upper bar colour android studio 
Java :: java replace second char of a string 
Java :: how to implement count steps in android 
Java :: replacing string with dynamic avalues java 
Java :: how to return custom value in api spring boot 
Java :: How tomake teris in Java 
Java :: last element array java 
Java :: sealed class in kotlin example 
Java :: Java byte Keyword 
Java :: get alpha from image java 
Java :: java.lang.number is interface or abstract class 
Java :: centrar valores en celda jtable java netbeans 
Java :: reset android studio settings windows 
Java :: The create method: 
Java :: use scanner class in global scope java 
Java :: .int x=5; final int y=7; x=x+10; y=y+10; 
Java :: imagebutton get background color programmatically 
Java :: mock ioexception mockito on BufferedReader 
Java :: android studio doesnt work when in full screen mac os 
Java :: how to get the url after loading page in webview in andorid 
Java :: buffered reader for big integer 
Java :: all GlideException#logRootCauses(String) for more detail Cause (1 of 1): class javax.net.ssl.SSLPeerUnverifiedException: Hostname 
Java :: android java update image dynamically 
Java :: first method in jdbc 
Java :: Java catch Keyword 
Java :: test spring cloud stream 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =