Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

matrix program

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 setRawsAndColumns(){
        System.out.println("Enter numbers of raws for array#1");
        int r1=scanner.nextInt();
        this.rawForArr1 =r1;
        System.out.println("Enter numbers of columns for array#1");
        int c1=scanner.nextInt();
        this.columnForArr1=c1;
        System.out.println("Enter numbers of raws for array#2");
        int r2=scanner.nextInt();
        this.rawForArr2 = r2;
        System.out.println("Enter numbers of columns for array#2");
        int c2=scanner.nextInt();
        this.ColumnForArr2 = c2;
        checkIfMatrixIsValid(c1,r2,r1,c2);
    }


    public void setRawsAndColumns(int r1, int c1, int r2, int c2){
        this.rawForArr1 =r1;
        this.columnForArr1=c1;
        this.rawForArr2 = r2;
        this.ColumnForArr2 = c2;
        checkIfMatrixIsValid(c1,r2,r1,c2);
    }



    public static void printValue(int[][] arr)
    {
                System.out.println(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 raws in the right array"+"
"+
                    "enter other value, this value 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[][] multiplied =multiplieMatrix(firstArr,secondArr);
        printValue(multiplied);



    }

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

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

    }

    public static int [][] multiplieMatrix(int [][] leftArr,int[][] rightArr){
        int m = leftArr.length;
        int n = rightArr[0].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%2>=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[0].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 :: java join if string is empty delimiter not used 
Java :: 8233*4 
Java :: leetspeak 
Java :: how to create 2d arrazlist in java 
Java :: how to come from 2nd fragment to first fragment android 
Java :: android studio press button programmatically 
Java :: What is the difference between Error and Exception? java 
Java :: hello world in bukkit 
Java :: implement hashmap 
Java :: factorial java recursion 
Java :: 2.5g ethernet linux problem 
Java :: Path to home directory java 
Java :: 710*12 
Java :: create a class 
Java :: what is a producedure java 
Java :: multi threading callable 
Java :: time in java 
Java :: java bild skalieren bufferedimage 
Java :: HQL . getFirstResult() 
Java :: public class Hello { public static void main( String[] args ){ System.out.println("Hello World"); } } 
Java :: stackoverflow java enum with constructor 
Java :: Java (HotSpot 12) sample 
Java :: void add method using collections 
Java :: java-util of geofence polygon 
Java :: material design implement full screen dialog android java 
Java :: java gui refresh frame 
Java :: libgdx load file 
Java :: value of for loop i in Jbutton 
Java :: java find nth smallest element using priority queue heap 
Java :: cypher query having multiple values 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =