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.

  • 1.Write a Java program that computes the average of the values of a row in a...

    1.Write a Java program that computes the average of the values of a row in a twodimensional array. You should create a method with the following header: public static double averageRow(double[][] array, int row) Write a test program that reads a matrix from the user and a row index to calculate the average on. Print the result. 2.A square matrix is said to be an upper triangular matrix if the value of the cell is 0 when row > column....

  • 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.

  • Write a Java program to prompt for inputting an integer N, then enter N integers (a...

    Write a Java program to prompt for inputting an integer N, then enter N integers (a loop is needed), print the numbers user entered, and the amount of even and odd numbers (zero is even number). (1) Prompt for the user to input an integer and output the integer. (1 pts) Enter an integer: You entered: 5 (2) Prompt for the user to input N integers, output the numbers entered and the amount of even and odd numbers (9 pts)...

  • Write a program to find product of a row vector, U having dimension 1 x 2...

    Write a program to find product of a row vector, U having dimension 1 x 2 with a 2 x 2 matrix, A. The input to the program includes initialized row vector, U and matrix, A. The expected output is a transformed row vector. You can test your program using following example: A = 2 4 5 1 4 5 1 and U = [3 2] The result of product U.A is a transformed row vector: [16 14] Q2. Write...

  • 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;...

  • 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...

  • 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...

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