Java Programs


Transpose of an Array

  • Program to obtain the transpose of an array. The transpose is obtained by exchanging the elements of each row with the elements of the corresponding column.
Program

import java.util.Scanner;

public class Transpose {

            private static Scanner sc;
            public void trans(int[][] matrix,int m) {
                       
                        int[][] transpose=new int[m][m];
                        for(int i=0;i<m;i++) {
                                    for(int j=0;j<m;j++) {
                                                transpose[i][j]=matrix[j][i];
                                    }
                        }
                        System.out.println("The transpose of the matrix is:");
                        for(int i=0;i<m;i++) {
                                    for(int j=0;j<m;j++) {
                                                System.out.print(transpose[i][j]+"\t");
                                    }
                                    System.out.println();
                        }
                       
            }
            public static void main(String[] args) {
                       
                        int m;
                        sc=new Scanner(System.in);
                        System.out.println("Enter the limit of the of the m x m matrix:");
                        m=sc.nextInt();
                        int[][] matrix=new int[m][m];
                        System.out.println("Enter the elements of the array:");
                        for(int i=0;i<m;i++) {
                                    for(int j=0;j<m;j++) {
                                                matrix[i][j]=sc.nextInt();
                                    }
                        }
                        Transpose ob=new Transpose();
                        ob.trans(matrix,m);

            }

}

Output

Enter the limit of the of the m x m matrix:
3
Enter the elements of the array:
1 2 3
4 5 6
7 8 9
The transpose of the matrix is:
1          4          7         
2          5          8         
3          6          9         


Perform Addition, Subtraction and Multiplication on two Arrays

  • Program which allow to perform any of the following operations on two arrays:
a.Add Arrays
b.Multiply Arrays
c.Subtract Arrays

Program

import java.util.Scanner;

public class ArrayMaths {

            private static Scanner sc;
            private static int m,n,x,y;
            public void add(int[][] matrix1,int[][] matrix2) {
                        System.out.println("The Array after addition is:");
                        for(int i=0;i<m;i++) {
                                    for(int j=0;j<n;j++) {
                                                matrix1[i][j]=matrix1[i][j]+matrix2[i][j];
                                                System.out.print(matrix1[i][j]+"\t");
                                    }
                                    System.out.println();
                        }
            }
           
            public void subtract(int[][] matrix1,int[][] matrix2) {
                        System.out.println("The Array after subtraction is:");
                        for(int i=0;i<m;i++) {
                                    for(int j=0;j<n;j++) {
                                                matrix1[i][j]=matrix1[i][j]-matrix2[i][j];
                                                System.out.print(matrix1[i][j]+"\t");
                                    }
                                    System.out.println();
                        }
            }
           
            public void multiply(int[][] matrix1,int[][] matrix2) {
                        int[][] matrix3=new int[m][y];
                        if(n==x) {
                                    for(int i=0;i<m;i++) {
                                                for(int j=0;j<y;j++) {
                                                matrix3[i][j]=0;
                                                for(int k=0;k<n;k++) {
                                                            matrix3[i][j]+=matrix1[i][k]*matrix2[k][j];
                                                           
                                                }
                                                System.out.print(matrix3[i][j]+"\t");
                                                }
                                                System.out.println();
                                    }
                        }
                        else {
                                    System.out.println("Multiplication cannot be performed.");
                                    System.out.println("No. of columns of first matrix should be equal to no. of rows of second matrix!!");
                        }
            }
           
            public static void main(String[] args) {
                       
                        int choice;
                        int l;
                        sc=new Scanner(System.in);
                        ArrayMaths ob=new ArrayMaths();
                        do {
                        System.out.println("Enter your choice:\n1.Add\t2.Subtract\t3.Multiply");
                        choice=sc.nextInt();
                        System.out.println("Enter the no. of rows and columns of the First Array:");
                        m=sc.nextInt();
                        n=sc.nextInt();
                        System.out.println("Enter the no. of rows and columns of the Second Array:");
                        x=sc.nextInt();
                        y=sc.nextInt();
                        if(choice==1||choice==2) {
                                    if(m!=x||n!=y) {
                                                System.out.println("Cannot perform the operation!!\nThe no. of columns should be same!!");
                                                return;
                                    }
                        }
                        else if(choice==3) {
                                    if(n!=x) {
                                                System.out.println("Multiplication cannot be performed!!");
                                                System.out.println("No. of rows of one array should be equal to no. of column of the other array");
                                                return;
                                    }
                        }
                        System.out.println("Enter the elements of the first Array:");
                        int[][] matrix1=new int[m][n];
                        for(int i=0;i<m;i++) {
                                    for(int j=0;j<n;j++) {
                                                matrix1[i][j]=sc.nextInt();
                                    }
                        }
                        System.out.println("Enter the elements of the second Array:");
                        int[][] matrix2=new int[x][y];
                        for(int i=0;i<x;i++) {
                                    for(int j=0;j<y;j++) {
                                                matrix2[i][j]=sc.nextInt();
                                    }
                        }
                        switch(choice) {
                        case 1: ob.add(matrix1,matrix2);
                                                break;
                        case 2: ob.subtract(matrix1,matrix2);
                                                break;
                        case 3: ob.multiply(matrix1,matrix2);
                                                break;
                        default            :           System.out.println("Invalid choice!!");
                                                            break;
                        }
                        System.out.println("Enter 1 to continue or 0 to exit!!");
                        l=sc.nextInt();
                        }while(l==1);

            }

}

Output

Enter your choice:
1.Add  2.Subtract        3.Multiply
1
Enter the no. of rows and columns of the First Array:
3 2
Enter the no. of rows and columns of the Second Array:
3 2
Enter the elements of the first Array:
1 2
3 4
5 6
Enter the elements of the second Array:
7 8
9 10
11 12
The Array after addition is:
8          10       
12        14       
16        18       
Enter 1 to continue or 0 to exit!!
1
Enter your choice:
1.Add  2.Subtract        3.Multiply
3
Enter the no. of rows and columns of the First Array:
3 2
Enter the no. of rows and columns of the Second Array:
2 3
Enter the elements of the first Array:
1 2
3 4
5 6
Enter the elements of the second Array:
1 2 3
4 5 6
9          12        15       
19        26        33       
29        40        51       
Enter 1 to continue or 0 to exit!!
0

No comments:

Post a Comment