Search This Blog

Showing posts with label matrix. Show all posts
Showing posts with label matrix. Show all posts

Java Program For Matrix Multiplication

This is a java program for Matrix Multiplication. To understand how it works, you should first know how matrix multiplication is done mathematically.

import java.util.Scanner;

public class MatrixMult{

public static void main(String[] args)
{
int[][] m1=new int[10][10];
int[][] m2=new int[10][10];
int[][] prod=new int[10][10];
int i,j,k,r1,c1,r2,c2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of rows and columns of first matrix:");
r1=sc.nextInt();
c1=sc.nextInt();
System.out.println("Enter number of rows and columns of second matrix:");
r2=sc.nextInt();
c2=sc.nextInt();
if(r2==c1)
    {
    System.out.println("Enter elements of First matrix (row wise):");
    for(i=0;i<r1;i++)
        for(j=0;j<c1;j++)
            m1[i][j]=sc.nextInt();
    System.out.println("Matrix1 is :");
    for(i=0;i<r1;i++)
        {
        for(j=0;j<c1;j++)
            System.out.printf("%d ",m1[i][j]);
        System.out.printf("\n");
        }
    System.out.println("Enter elements of Second matrix (row wise):");
    for(i=0;i<r2;i++)
        for(j=0;j<c2;j++)
            m2[i][j]=sc.nextInt();
    System.out.println("Matrix2 is:");
    for(i=0;i<r2;i++)
        {
        for(j=0;j<c2;j++)
            System.out.printf("%d ",m2[i][j]);
        System.out.printf("\n");
        }
    System.out.println("Product of the Matrices (M1 x M2):");
    for(i=0;i<r1;i++)
        {
        for(j=0;j<c2;j++)
            {
            prod[i][j]=0;
            for(k=0;k<r1;k++)
                prod[i][j]+=m1[i][k]*m2[k][j];
            System.out.printf("%d ",prod[i][j]);
            }
        System.out.print("\n");
        }
    }
else
    {
    System.out.println("Matrices can't be multiplied.");
    System.out.print("No. of columns of first matrix and no of rows of second are different.");
    }
}
}

Output

Enter number of rows and columns of first matrix:
2
2
Enter number of rows and columns of second matrix:
2
2
Enter elements of First matrix (row wise):
5
8
6
4
Matrix1 is :
5 8 
6 4 
Enter elements of Second matrix (row wise):
5
6
8
9
Matrix2 is:
5 6 
8 9 
Product of the Matrices (M1 x M2):
89 102 
62 72 

Java Program For Spiral Matrix - Java Programming Interview Question

A spiral matrix is a matrix (two dimensional array) in which the elements are stored in a spiral fashion. Actually it is a normal NxN two dimensional array. But instead of accessing elements in row-wise or column-wise order, we access them in spiral order. i.e, in a 3x3 matrix the order of retrieval of elements is spiral order is as follows:

(0,0)  (0,1)   (0,2)  (1,2)   (2,2)   (2,1)   (2,0)   (1,0)   (1,1)

The following picture shows a 4x4 spiral matrix:
4x4 spiral matrix using two dimensional array - Java Program for spiral matrix
A 4x4 Spiral matrix

An question asked by zoho corporation during interview was to write a program to read a matrix spirally. In this post, we will answer this zoho interview question. Remember that the memory implementation is purely same as a normal matrix. The only difference here is the order in which we store elements to matrix or access elements in matrix. The following is the java program to read a spiral matrix. In thi s java program for spiral matrix, we just read the elements to the matrix. The elements entered by the user are entered into the matrix spirally. And the program finally displays the full matrix just as all normal matrices are displayed. Then you can see that the elements are not stored in the order as they were entered. You can see the spiral order in the matrix.

import java.io.*;
public class Spiral
{

public static void main(String args[])
{
int a[][]=new int[10][10];
int o,cellcount,cc=0,c=0,i=0,j=0,g=1;
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the order of spiral matrix");
o=Integer.parseInt(br.readLine());
cellcount=o;
System.out.println("\nEnter the elements:\n");
while(c<o*o)
    {
    cc=0;
    for(;cc<cellcount;j=j+g,cc++)
	a[i][j]=Integer.parseInt(br.readLine());
    j=j-g;
    c+=cc;
    cc=0;
    i=i+g;
    cellcount--;
    if(c>=o*o)
	break;
    for(;cc<cellcount;i=i+g,cc++)
	a[i][j]=Integer.parseInt(br.readLine());
    c+=cc;
    i=i-g;
    j=j-g;
    g*=-1;
    }
System.out.println("\nThe spiral matrix is:");
for(i=0;i<o;i++)
    {
    System.out.print("\n");
    for(j=0;j<o;j++)
	System.out.print(Integer.toString(a[i][j])+"\t");
    }
}catch(Exception ex){ex.printStackTrace();}
}

}