using array

using array

transpose a matrix?

View Answers

May 25, 2012 at 4:09 PM

Java find transpose of matrix

The given code determine the transpose of a matrix.The transpose of a matrix is formed by interchanging the rows and columns of a matrix such that row i of matrix becomes column i of the transposed matrix. The transpose of A is denoted by AT. Through the following code,the user is allowed to enter the number of rows and columns and the elements to create a matrix. Then using the for loop statements, turn all the rows of the matrix into columns and vice-versa.

import java.util.*;

    public class Transpose {
            public static void main(String[] args) throws Exception {
                    int rows, cols;
                    int[][] matrix, tmatrix;
                    Scanner input = new Scanner(System.in);
                    System.out.print("Enter number of rows: ");
                    rows = input.nextInt();
                    System.out.print("Enter number of columns: ");
                    cols = input.nextInt();
                    matrix = new int[rows][cols];
                    tmatrix = new int[cols][rows];
                    System.out.println("Enter elements for Matrix");
                    for (int i = 0; i < rows; i++) {
                            for (int j = 0; j < cols; j++) {
                                    matrix[i][j] = input.nextInt();
                            }
                    }
                    for (int i = 0; i < rows; i++) {
                            for (int j = 0; j < cols; j++) {
                                    tmatrix[i][j] = matrix[j][i];
                            }
                    }
                    System.out.println("Matrix is:");
                    for (int i = 0; i < rows; i++) {
                            for (int j = 0; j < cols; j++) {
                                    System.out.print(matrix[i][j] + " ");
                            }
                            System.out.println();
                    }
                    System.out.println("Its transpose is: ");
                    for (int i = 0; i < cols; i++) {
                            for (int j = 0; j < rows; j++) {
                                    System.out.print(tmatrix[i][j] + " ");
                            }
                            System.out.println();
                    }
            }
    }

Ads









Related Tutorials/Questions & Answers:
using array
using array  Circular left shift array element by one position
using array
using array  Circular left shift array element by one position
Advertisements
using array
using array  display 10 digit number using array and print pyramid. 1 1 1 1 1 1 1 1 1 1 1 1
using array
using array  transpose a matrix
using array
using array  transpose a matrix
using array
using array  column wise total of a matrix
using array
using array  read 10 digit number and display (star
using 2D Array
using 2D Array  Write a JAVA program using 2D Array to auto-grade exams. For a class of N students, your program should read letter answers (A, B, C... the class average. Define the exam answer key as Final Array in your program
using 2D Array
using 2D Array  Write a JAVA program using 2D Array to auto-grade exams. For a class of N students, your program should read letter answers (A, B, C... the class average. Define the exam answer key as Final Array in your program
Java Programming using array
Java Programming using array  I'm creating a programming using array... Program: import java.io.*; public class Array { public static void main(String args...','H','J','K','L','M','N','P','Q','R','S','T','V','W','X','Y','Z'}; char ch
Finding duplicates using array in for loop
Finding duplicates using array in for loop  how to find the duplicates in array using for loop
one dimensional array using java
one dimensional array using java  design and implement a java program that will read a file containing numbers and compute the following statistics: the range(low,high) the average and the median
Using [] operator of EL with an Array
Using [ ] operator of EL with an Array   .... An array is a type of container which can hold a fixed number of values of a single type. Array is a collection of similar data types. It means if you have declared
example to booking ticket cinema using array
example to booking ticket cinema using array   example to booking ticket cinema using array
How to using Binary Search Array Java ?
How to using Binary Search Array Java ?  Hi, I am beginners in Java Programming Language. I am trying to create small application using Array functions. The problem is that how to use binary search array in Java. Please give
how to create a zip by using byte array
how to create a zip by using byte array  hi, How to convert byte array to zip by using java program.can u plz provide it...... Thanks, krishna
How to save array of UserType in Oracle using Hibernate.
How to save array of UserType in Oracle using Hibernate.  Hi How to save array of UserType in Oracle using Hibernate. CREATE OR REPLACE TYPE...()); ARRAY array =new ARRAY(arrdesc, arg0.getConnection(), null
How to create binary search tree using an array?
How to create binary search tree using an array?  hello people, pls guide me on the topic above. i have an string array, i want to make a binary search tree based on data inside this array. the array contains names of people
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
Display tow dimensional array using one for loop in java
Display tow dimensional array using one for loop in java  Display tow dimensional array using one for loop in java
Convert a string representation of a hex dump to a byte array using Java?
Convert a string representation of a hex dump to a byte array using Java?  Convert a string representation of a hex dump to a byte array using Java
how to Insert Array Values Into Separate Rows using java?
how to Insert Array Values Into Separate Rows using java?  how to Insert Array Values Into Separate Rows using java?  class...) { String array[][]={{"1","Angel","Delhi"},{"2","John","Chennai"},{"3","William
Display tow dimensional array by matrix form using one for loop in java
Display tow dimensional array by matrix form using one for loop in java  Display tow dimensional array by matrix form using one for loop in java
using getText() to retrieve text from an array of text fields with loop.
using getText() to retrieve text from an array of text fields with loop.   hi, I have an array of about 50 textfields which show up when somebody presses button. Now I want to get the text from all the textfields using array
Calculating the checksum of a Byte Array by using Adler32
Calculating the checksum of a Byte Array by using Adler32... of a Byte Array using Adler32. Adler32: It is a error checking technique used... field whose type is byte. To calculate the Checksum of the Byte Array by using
Access value of array using OGNL in struts2.
Access value of array using OGNL in struts2. Here, you will see how to access value of  array in struts2 using OGNL.  1-index.jsp <%@taglib...] : <s:property value="name[0]"/><br/> Value array list
how to convert string to char array in java without using tochararray
how to convert string to char array in java without using tochararray ...() function. how to convert string to char array in java without using... to char array in java without using tochararray: package my.app; public
Array of String using Pointers
Array of String using Pointers       In this section, you will learn how to create an array of string using pointers in C. The declaration of an array of character pointers
Array
Array  How do i insert elements into an array up to a limit from command prompt using SOP statement.like Enter range:3 Enter elements: 1 5 3  ...("Enter Range: "); int size=input.nextInt(); int array[]=new int[size
in_array
in_array  in_array in php

Ads