Question

Bonus 1 • Write a java program to transpose a matrix mat[N][M], where: 1) You ask the user to input N and M 2) You then ask t

1 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class Transpose {

    public static void transpose(int[][] arr) {
        int temp;
        for (int i = 0; i < arr.length; ++i) {
            for (int j = 0; j < i; ++j) {
                temp = arr[i][j];
                arr[i][j] = arr[j][i];
                arr[j][i] = temp;
            }
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number of rows: ");
        int N = in.nextInt();
        System.out.print("Enter number of columns: ");
        int M = in.nextInt();
        int[][] arr = new int[N][M];
        for (int i = 0; i < arr.length; ++i) {
            for (int j = 0; j < arr[i].length; ++j) {
                System.out.print("Enter an element at row " + i + " and column " + j + ": ");
                arr[i][j] = in.nextInt();
            }
        }
        transpose(arr);
        System.out.println("Transposed matrix is");
        for (int i = 0; i < arr.length; ++i) {
            for (int j = 0; j < arr[i].length; ++j) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Enter number of rows: 3 Enter number of columns: 4 Enter an element at row 0 and column 0: Enter an element at row 0 and colu

Add a comment
Know the answer?
Add Answer to:
Bonus 1 • Write a java program to transpose a matrix mat[N][M], where: 1) You ask...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 9) [6 Pts) Write a code to transpose the elements of an input matrix. You may...

    9) [6 Pts) Write a code to transpose the elements of an input matrix. You may definen and use the function rand(n,n) to create the input matrix. Output: Original matrix and the transposed matrix. Do NOT use the transpose operatorlll

  • , where ' represents the transpose of the matrix, an n × m matrix of full...

    , where ' represents the transpose of the matrix, an n × m matrix of full rank (a) What is HX in terms of X's? Simplify as much as possible. (b) What are the dimensions of the matrix H if X is a 4 × 2 matrix? (c) What are the dimensions of the matrix H if X is an n × m matrix? (d) Find trace(H). Hint: Trace has a special property involving cyclic permutations.

  • Write a Java program that calculates the sum of a variable sized matrix using a two...

    Write a Java program that calculates the sum of a variable sized matrix using a two dimensional array. (ArraySum.java) The user should provide the number of rows and columns Test multiple user inputs (3 times 2, 4 times 4, 6 times 2, etc) A sum should be created for each row, each column, and the total for the matrix Ex.: How big would you like your matrix? Rows - ? 3 Columns -? 2 Please enter your row 1? Column...

  • JAVA Program

    1. Write a java program to find the sum of the elements on the diagonal of a matrix. 2. Write a java program to add two matrices. 3. Write a java program to find a given value in a matrix. 4. Write a java program to multiply two matrices. 5. Write a java program to find the transpose of a matrix.

  • Question 4 (3 mark) : Write a Java program to ask the user to input an...

    Question 4 (3 mark) : Write a Java program to ask the user to input an integer number from the keyboard. The output should indicate whether it is positive, zero, or negative, an even number or an odd number. REQUIREMENTS • Your code should ask user to input an integer number, then decide its sign and parity based on this input. • Your code should use if-else statement. Your code must work exactly as the specification and the output should...

  • Write a function Transpose that transposes a matrix T with M rows and N colums. The...

    Write a function Transpose that transposes a matrix T with M rows and N colums. The transposed matrix, TT, should have N rows and M columns. Example. Given the matrix T. (C++14) Example: 1 2 3 0 -6 7 The transposed matrix TT is 1 0 2 -6 3 7 DEFAULT CODE: Add to code as needed: #include <iostream> #include <string> #include <cmath> using namespace std; void Transpose(int T[100][100], int TT[100][100], int M, int N) { int i; int j;...

  • 4A. Write a CUDA host program that reads a character type matrix A and integer type matrix B of size mxn. It produces an output string STR such that, every character of A is repeated n times (where n...

    4A. Write a CUDA host program that reads a character type matrix A and integer type matrix B of size mxn. It produces an output string STR such that, every character of A is repeated n times (where n is the integer value in matrix B which is having the same index as that of the character taken in A). Solve this using 1D Block. Example: 1 2 4 3 e X a M Output String STR: pCCaaaaPPPeeXXXXaaaMM 4B. Write...

  • JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matric...

    JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matrices have N rows and M columns, N>1 and M>1. The two matrices must be divided to four equal size sub-matrices and each sub-matrix has dimensions N/2 X M/2. I need to create four threads and each thread performs a sub-set of addition on one pair of the sub-matrices. I also need an extra thread for networking. The network part of this uses...

  • Write a java program that will print if n numbers that the user will input are...

    Write a java program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times asking for an integer...

  • Write a program that will check a Java file for syntax errors. The program will ask...

    Write a program that will check a Java file for syntax errors. The program will ask for an input-file name and output-file name and will then copy all the code from the Java input file to the Java output file, but with the following changes: 1. Any syntax error found in the Java input file will be corrected: a. Missing semicolon b. Missing compound statements (curly braces) c. All comments have to start with // and end with a period....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT