Transposée d'une matrice [Java]

Fermé
Kbibi - 16 déc. 2011 à 10:40
Char Snipeur Messages postés 9696 Date d'inscription vendredi 23 avril 2004 Statut Contributeur Dernière intervention 3 octobre 2023 - 3 janv. 2012 à 11:43
Bonjour,

Je souhaite réalisé la transposée d'une matrice. Je n'ai aucune erreur mais mon programme ne fonctionne pas, pouvez vous m'aidez ?

Voici mon scripte Java :
Programme principal :
Scanner entrée = new Scanner(System.in);
int nbl1; // lignes de la matrice M1
int nbc1; // colonnes de la matrice M1
Matrice transposée=new Matrice();
Matrice M1;
M1 = new Matrice();
System.out.println("Entrez le nombre de lignes de la première matrice : " );
M1.ligne=entrée.nextInt();
System.out.println("Entrez le nombre de colonnes de la première matrice : ");
M1.colonne=entrée.nextInt();
M1.Mat= new int [M1.ligne][M1.colonne];
System.out.println("Votre première matrice est de dimension : (" + M1.ligne +"," +M1.colonne +")");
// saisie de la première matrice :
for (nbl1=0;nbl1<M1.ligne;nbl1++)
for (nbc1=0;nbc1<M1.colonne;nbc1++)
M1.Mat[nbl1][nbc1]=entrée.nextInt();
// affichage de la première matrice :
System.out.println("Votre première matrice est : ");
for (nbl1=0;nbl1<M1.ligne;nbl1++) {
for (nbc1=0;nbc1<M1.colonne;nbc1++)
System.out.print(" " + M1.Mat[nbl1][nbc1] );
System.out.println(); }
//appel de la fonction transposé :
transposée=matrice_transposée(M1);
System.out.println("La transposée de votre matrice est : ");


Fonction transposée :
public static Matrice matrice_transposée (Matrice M) {
Matrice tmp=new Matrice();
int i,j;
for (i=0; i<=M.ligne; i++)
for (j=0; j<=M.colonne; j++)
M.Mat[j][i]=tmp.Mat[i][j];
return tmp; }

Merci.

1 réponse

assasin altair Messages postés 4 Date d'inscription lundi 2 janvier 2012 Statut Membre Dernière intervention 2 janvier 2012 3
2 janv. 2012 à 20:52
public static Matrice matrice_transposée (Matrice M) {
Matrice tmp=new Matrice();
tmp.ligne = M.colonne;
tmp.colonne = M.ligne;
tmp.mat = new int[tmp.ligne][tmp.colonne];
for (int i=0; i<M.ligne; i++)
for (int j=0; j<M.colonne; j++)
tmp.Mat[i][j] = M.Mat[j][i];
return tmp;
}
3
Char Snipeur Messages postés 9696 Date d'inscription vendredi 23 avril 2004 Statut Contributeur Dernière intervention 3 octobre 2023 1 297
3 janv. 2012 à 11:43
pour résumer la correction : dimensionner correctement la matrice transposée; puis faire l'affectation dans le bon sens.
D'ailleurs, j'aurai plutôt écris:
for (int i=0; i< tmp.ligne; i++)
for (int j=0; j< tmp.colonne; j++)
   tmp.Mat[i][j] = M.Mat[j][i]; 
0