Java >> Tutoriel Java >  >> Java

Programme Java pour afficher la transposée d'une matrice

Dans ce tutoriel, nous allons apprendre à afficher la transposée d'une matrice. Mais avant d'aller plus loin, si vous n'êtes pas familier avec les concepts du tableau, alors consultez l'article Tableaux en Java.

Entrée : Saisissez les éléments de la matrice :

1 2 3

4 5 3

9 3 2

Sortie : Transposer La matrice est :

1 4 3

2 5 3

3 3 2

Programme 1 :Afficher la transposée d'une matrice

Dans ce programme, nous utiliserons une matrice séparée pour stocker les éléments de transposition.

Algorithme

  1. Démarrer
  2. Déclarez des variables pour les lignes et les colonnes de la matrice.
  3. Demandez à l'utilisateur d'initialiser les lignes et les colonnes.
  4. Déclarez une matrice.
  5. Demandez à l'utilisateur d'initialiser les éléments de la matrice.
  6. Imprimez la matrice d'origine.
  7. Déclarez une autre matrice qui stockera l'élément de matrice de transposition.
  8. Stockez les éléments dans la matrice de transposition en modifiant les lignes et les colonnes de la matrice d'origine.
  9. Afficher la matrice de transposition.
  10. Arrêtez.

Vous trouverez ci-dessous le code correspondant.

Le programme ci-dessous montre comment trouver la transposition d'une matrice.

/*JAVA PROGRAM TO DISPLAY THE TRANSPOSE OF A MATRIX*/
import java.util.*;

public class Main
{
     public static void main(String []args)
     {
         ///Take input from the user
         Scanner sc=new Scanner(System.in);
         
         int m,n;                 //Matrix Size Declaration
         
         System.out.println("Enter the number of rows: \n");
         m=sc.nextInt();  //Matrix Size Initialization
         
         System.out.println("Enter the number of column: \n");
         n=sc.nextInt();  //Matrix Size Initialization
         
         int arr[][]=new int[10][10];        //Matrix Size Declaration
         System.out.println("Enter the elements of the matrix: ");
         for(int i=0;i<m;i++)    //Matrix Initialization
         {
            for(int j=0;j<n;j++)
            {
                 arr[i][j]=sc.nextInt();
            }
         }
         
         //Print the original Matrix
         System.out.println("The elements in the original matrix are: ");
         for(int i=0;i<m;i++)     //Print the matrix
         {
             for(int j=0;j<n;j++)
             {
                  System.out.print(arr[i][j]+" ");
             }
            System.out.println("");
        }
        
        int brr[][]=new int[10][10];        //Transpose Matrix Declaration
        for(int i=0;i<m;i++)     //Transpose Matrix initialization
        {
             for(int j=0;j<n;j++)
             {
                 brr[j][i]=arr[i][j];     //Store elements in the transpose matrix
             }
        }
        
        System.out.println("After transposing the elements are...");
        for(int i=0;i<m;i++)      //Print the transpose matrix
        {
             for(int j=0;j<n;j++)
             {
                 System.out.print(brr[i][j]+" ");
             }
            System.out.println("");
        }
         
     }
}


Entrez le nombre de lignes :3
Entrez le nombre de colonne :3
Entrez les éléments de la matrice :5 4 3 1 2 6 9 8 7
Les éléments de la matrice originale sont :
5 4 3
1 2 6
9 8 7
Après transposition, les éléments sont...
5 1 9
4 2 8
3 6 7

Programme 2 :Afficher la transposée d'une matrice

Dans ce programme, nous utiliserons la même matrice mais lors de l'impression nous modifierons la position des éléments.

Algorithme

  1. Démarrer
  2. Déclarez des variables pour les lignes et les colonnes de la matrice.
  3. Demandez à l'utilisateur d'initialiser les lignes et les colonnes.
  4. Déclarez une matrice.
  5. Demandez à l'utilisateur d'initialiser les éléments de la matrice.
  6. Imprimez la matrice d'origine.
  7. Pour imprimer la matrice de transposition, modifiez les positions des lignes et des colonnes.
  8. Afficher la matrice.
  9. Arrêter

Vous trouverez ci-dessous le code correspondant.

Le programme ci-dessous montre comment afficher la transposition d'une matrice sans utiliser d'autre tableau.

/*JAVA PROGRAM TO DISPLAY THE TRANSPOSE OF A MATRIX*/
import java.util.*;

public class Main
{
     public static void main(String []args)
     {
         ///Take input from the user
         Scanner sc=new Scanner(System.in);
         
         int m,n;                 //Matrix Size Declaration
         
         System.out.println("Enter the number of rows: \n");
         m=sc.nextInt();  //Matrix Size Initialization
         
         System.out.println("Enter the number of column: \n");
         n=sc.nextInt();  //Matrix Size Initialization
         
         int arr[][]=new int[10][10];        //Matrix Size Declaration
         
         System.out.println("Enter the elements of the matrix: ");
         for(int i=0;i<m;i++)    //Matrix Initialization
         {
            for(int j=0;j<n;j++)
            {
                 arr[i][j]=sc.nextInt();
            }
         }
         
         //Print the original Matrix
         System.out.println("The elements in the original matrix are: ");
         for(int i=0;i<m;i++)     //Print the matrix
         {
             for(int j=0;j<n;j++)
             {
                  System.out.print(arr[i][j]+" ");
             }
            System.out.println("");
        }
        
        //Print the transpose matrix without creating any new matrix
        System.out.println("After transposing the elements are...");
        for(int i=0;i<m;i++)      
        {
             for(int j=0;j<n;j++)
             {
                 System.out.print(arr[j][i]+" ");
             }
            System.out.println("");
        }
         
     }
}


Entrez le nombre de lignes :3
Entrez le nombre de colonne :3
Entrez les éléments de la matrice :9 8 7 6 7 2 3 1 3
Les éléments de la matrice originale sont :
9 8 7
6 7 2
3 1 3
Après transposition, les éléments sont...
9 6 3
8 7 1
7 2 3


Balise Java