Transpose of a Matrix – ARRAY in JAVA

Description


First, what is an ARRAY?

Actually, Array is a group of elements having similar data type (either int, char, float, etc.) with continuous memory allocation.

Arrays are of 2 types –

1.)    Single-Dimensional (having only one dimension either row or either column)

–> Declaration – <data type><variable name> = new<data type>[no. of elements];

–> Example – int arr[] = new int[10], means an integer type array arr containing 10 elements.

2.)    Multi-Dimensional (having more than one dimensions either more than one rows or either more than one columns).

–> Declaration – <data type><variable name>[][] = new<data type> [no. of rows][no. of columns];

–> Example – int arr[][] = new int[10][10], means an integer type array arr containing 10 rows & 10 columns.

S.No. Types of Declarations of Array in JAVA (Examples)
1. int arr[] = new int [5]
2. int arr[] = {1,2,3,4,5}
3. int arr[] = new int[]{1,2,3,4,5}

[/table]

Now here we are going to transpose the array, means

If the inputted array is –

matrix_before_transpose

Then,

the output should be –

matrix_after_transpose

Means, rows & columns should be interchanged.

For transforming matrix, first we need to take the input of rows & columns in the variable r & c. Also we have to initialize the array variable with any of the value as here we have arr[100][100] means 100 rows & 100 columns. Now what we are doing here is, changing the rows with the columns. Here we are running a for loop with variable ifor rows & variable j for columns. At last we are just changing the variables i à j and j à i.

As in the program, arr[i][j] is changed to arr[j][i].

_____________________________________________________

Also, Here we have used “Scanner Class” for inputting the data.

Syntax of Scanner Class is : 

import java.util.scanner.*;

Scanner sc=new Scanner(System.in);

if int ‘i’ is an integer variable, then for taking the input we will write the code as – 

i=sc.nextInt();

SOURCE CODE
SOURCE CODE

Author Description

humanz

Our mission is to bring the affordable Technology education to people everywhere, in order to help them achieve their dreams and change the world. - Follow @program_guide

No comments yet.

Join the Conversation