You are on page 1of 5

JAVA ASSIGNMENT 3

NAME- SHIRISH WAGHMODE


REG. NO- 19MIM10026
SLOT- D21

 Create a class Matrix and demonstrate the use of member functions to perform the
following: 

1. Addition of two matrices


2. Multiplication of two matrices.
3. Transpose of a matrix
4. Sort each row of the matrix in increasing order of value.

Solution

1.
public class Matrix{
public static void main(String args[])
{
//create two 3*3 matrices with arbitrary values
int a[][]={{1,2,3},{4,5,6},{3,4,5}};
int b[][]={{1,2,4},{1,4,5},{1,1,4}};

//create another matrix to store the sum of two matrices


int c[][]=new int[3][3]; //3 rows and 3 columns

//adding and printing addition of 2 matrices


for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j]; //use - for subtraction
System.out.print(c[i][j]+" ");
}
}
}}

2.
public class Matrix{
public static void main(String args[]){
//create two 3*3 matrices with arbitrary values
int m1[][]={{1,2,3},{4,5,6},{2,3,4}};
int m2[][]={{1,2,3},{4,5,6},{2,3,4}};
int m[][]=new int[3][3]; //3 rows and 3 columns
//operating multiplication
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
m[i][j]=0;
for(int k=0;k<3;k++)
{
m[i][j]+=m1[i][k]*m2[k][j];
}
//end of k loop
System.out.print(m[i][j]+" "); //printing matrix
}
//end of j loop
System.out.println();
}
}}

3.
public class MatrixTranspose
{
public static void main(String args[])
{
int i, j;
System.out.println("Enter total rows and columns: ");
Scanner s = new Scanner(System.in);
int row = s.nextInt();
int column = s.nextInt();
int array[][] = new int[row][column];
System.out.println("Enter matrix:");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
array[i][j] = s.nextInt();
System.out.print(" ");
}
}
System.out.println("The above matrix before Transpose is ");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
System.out.print(array[i][j]+" ");
}
System.out.println(" ");
}
System.out.println("The above matrix after Transpose is ");
for(i = 0; i < column; i++)
{
for(j = 0; j < row; j++)
{
System.out.print(array[j][i]+" ");
}
System.out.println(" ");
}
}
}

4.
public class MatrixSort
{
public static void main(String args[])
{
int rows;
int cols;
Scanner input = new Scanner(System.in);
System.out.println("Enter number of rows u want in matrix ");
rows=input.nextInt();
System.out.println("Enter number of columns u want in matrix ");
cols=input.nextInt();
int Numbers[][]=new int[rows][cols];
int temp;
//get input
System.out.println("Enter integer values ");
for (int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
Numbers[i][j]=input.nextInt();
}
}
for(int i=1;i<rows;i++)
{

for(int a=1;a<cols;a++)
{
for(int j=1;j<cols-1;j++)
if(Numbers[i][j]>Numbers[i][j+1])
{
temp = Numbers[i][j+1];
Numbers[i][j+1] = Numbers[i][j];
Numbers[i][j] = temp;

}
}
}
/*for(int i=0;i<rows;i++)
{

for(int j=0;j<cols-1;j++)
{
if(Numbers[i][j]>Numbers[i][j+1])
{
temp = Numbers[i][j];
Numbers[i][j] = Numbers[i][j+1];
Numbers[i][j+1] = temp;

}
}
}*/
System.out.println("sorted numbers in ascending order are : ");
for (int i=0;i< rows;i++)
{
for (int j=0;j< cols;j++)
{
System.out.print(Numbers[i][j]+" ");
}
System.out.println();
}
}
}

You might also like