Answer to Question #322946 in Java | JSP | JSF for Nhm

Question #322946

Required Object Class and Driver Class , Write a Java program to obtain transpose of a 4 x 4 matrix. The Transpose of the matrix is obtained by exchanging the elements of each row. With the elements of the corresponding column. 


1
Expert's answer
2022-04-04T02:40:41-0400
import java.util.Scanner;

class ObtainTranspose{
    int[][] arrT = new int[4][4];

    public void arrayTranspose (int[][] arr){

        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                arrT[j][i] = arr[i][j];
            }
        }
    }

    public void printArray(){

        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                System.out.printf("%5d", arrT[i][j]);
            }
            System.out.println("");
        }
    }


}

public class Main {

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        ObtainTranspose obTra = new ObtainTranspose();

        int[][] arr = {{85, 42, 11, 47}, {19, 2, 43, 7}, {1, 77, 54, 32}, {12, 22, 24, 26}};

        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                System.out.printf("%5d", arr[i][j]);
            }
            System.out.println("");
        }

        System.out.println("");
        obTra.arrayTranspose(arr);
        obTra.printArray();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS