Terminating Arrays With Program That Finds Out Transpose Of Entered Matrix [ Jeclipse Course 4 ]
What Will I Learn?
- You will learn how to use java for math problems.
- You will learn how to use arrays on math.
Requirements
- Eclipse And Java Oxygen Setup On Eclipse
- Basic Knowledge Of Coding
- Intermediate Knowledge Of Math
Difficulty
- Basic
Tutorial Contents
Hello,in this tutorial we are going to emphasize on math and arrays.I want you to figure out arrays on java very good.We are going to make a program that outputs the transposes of user entered matrix.Let's start with defining our needed library which is "java.util.scanner" as i told in first tutorial this library allows you to use "scanner".
After that i name my class as "mtrx" then defining four integers for the ahead of my program.
import java.util.Scanner;
public class Mtrx {
public static void main(String args[])
{
int a, b, c, d;
In this part i am creating new scanner called "mtx".(System.in) is used for to input stream in to system.Then i am going to ask user to enter the number of rows and columns of matrix.Then equalize my two integers to mtx.nextint."Nextint" nextInt method is used to read an int value from the keyboard and to assign to the variable also is used to get a random, uniformly distributed int value between 0 and the specified value,drawn from this random number generator's sequence.
Scanner mtx = new Scanner(System.in);
System.out.println("Enter rows and columns number of matrix");
a = mtx.nextInt();
b = mtx.nextInt();
Now i define new array and assign int matrix to a and b integers.
int matrix[][] = new int[a][b];
And i ask user to enter the matrix elements then in this part i will use for loop inside for loop to make system print out the numbers smooth,to do this we equalize our other two integers c and d to zero then define the condition and increase c and d if needed.
Then we defined our array named "matrix" in integer type at above we assign c and d and equalize to nextint to assign a new value.
System.out.println("Enter the matrix elements");
for ( c = 0 ; c < a ; c++ )
for ( d = 0 ; d < b ; d++ )
matrix[c][d] = mtx.nextInt();
Now i am defining a new array named "transpose" and assigning b and a integers to it.Then in below line we use for in for again and do the same thing we did on above this time to write out the transposes on users screen.Important part in here is "transpose[d][c] = matrix[c][d];" We assign each array to each other in here to make user enter elements of matrix correctly.
int transpose[][] = new int[b][a];
for ( c = 0 ; c < a ; c++ )
{
for ( d = 0 ; d < b ; d++ )
transpose[d][c] = matrix[c][d];
}
In the final step we show user the transpose of matrix's result and use for in for loop one more time.As you know how to find the transpose we set the rules in this loop to show user the result.Now finally we printout our "c" and "d" arrays and use \t to tab and make spaces between the numbers and using \n to go down a line.
System.out.println("Transpose of matrix is:");
for ( c = 0 ; c < b ; c++ )
{
for ( d = 0 ; d < a ; d++ )
System.out.print(transpose[c][d]+"\t");
System.out.print("\n");
In case if you forget how to find transpose.
Whole Code And Output
import java.util.Scanner;
public class Mtrx {
public static void main(String args[])
{
int a, b, c, d;
Scanner mtx = new Scanner(System.in);
System.out.println("Enter rows and columns number of matrix");
a = mtx.nextInt();
b = mtx.nextInt();
int matrix[][] = new int[a][b];
System.out.println("Enter the matrix elements");
for ( c = 0 ; c < a ; c++ )
for ( d = 0 ; d < b ; d++ )
matrix[c][d] = mtx.nextInt();
int transpose[][] = new int[b][a];
for ( c = 0 ; c < a ; c++ )
{
for ( d = 0 ; d < b ; d++ )
transpose[d][c] = matrix[c][d];
}
System.out.println("Transpose of matrix is:");
for ( c = 0 ; c < b ; c++ )
{
for ( d = 0 ; d < a ; d++ )
System.out.print(transpose[c][d]+"\t");
System.out.print("\n");
}
}
}
Curriculum
Place here a list of related tutorials you have already shared on Utopian that make up a Course Curriculum, if applicable.
Posted on Utopian.io - Rewarding Open Source Contributors
Your contribution cannot be approved because it does not follow the Utopian Rules.
Suggestions:
You can contact us on Discord.
[utopian-moderator]
Hey @roj, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!