1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
import java.util.Scanner;
public class matrice {
int mat [][];
int ligne, colonne;
matrice(int l, int c) {
ligne = l;
colonne = c;
mat = new int[ligne][colonne];
}
void remplissage() {
int i, j;
for (i = 0; i < ligne; i++)
for (j = 0; j < colonne; j++) {
System.out.println("donner M[" + i + "][" + j + "] ");
Scanner S = new Scanner(System.in);
mat [i][j] = S.nextInt();
}
}
// affichage de la matrice par lignes
void affichgeParligne() {
int i, j;
for (i = 0; i < ligne; i++)
for (j = 0; j < colonne; j++)
{System.out.print(mat[i][j] + " ");}
System.out.println();
}
// affichage de la matrice par colonnes
void affichgeParcolonne() {
int i, j;
for (i = 0; i < colonne; i++)
for (j = 0; j < ligne; j++)
{System.out.print(mat[j][i] + " ");}
System.out.println();
}
//La matrice fois m1
matrice somme(matrice m1) {
if ((m1.ligne == ligne) && (m1.colonne == colonne)) {
matrice res = new matrice(ligne, colonne);
int i, j;
for (i = 0; i < ligne; i++)
for (j = 0; j < colonne; j++)
res.mat[i][j] = mat[i][j] + m1.mat[i][j];
return res;
}
return null;
}
// Produit de la matrice courante par m1
matrice produit(matrice m1) {
if (colonne == m1.ligne) {
matrice res = new matrice(ligne, m1.colonne);
int i, k, j, s = 0;
for (k = 0; k < ligne; k++)
for (j = 0; j < m1.colonne; j++)
{
s = 0;
for (i = 0; i < colonne; i++)
s += mat[k][i] * m1.mat[i][j];
res.mat[k][j] = s;
}
return res;
} else
return null;
}
//la ligne l de la matrice * k
void modif1(int l, int k) {
if ((l >= 0) && (l < ligne)) {
for (int i = 0; i < colonne; i++)
mat[l][i] = k * mat[l][i];
}
}
// La ligne l reçoit sa valeur + k * la ligne l1
void modif2(int l, int l1, int k) {
if ((l >= 0) && (l < ligne) && (l1 >= 0) && (l1 < ligne)) {
for (int i = 0; i < colonne; i++)
mat[l][i] = (k * mat[l1][i]) + mat[l][i];
}
}
int getindice(int l) {
int max = 0;
int ind = 0;
if ((l >= 0) && (l < ligne)) {
for (int i = 0; i < colonne; i++)
if (max > mat[l][i]) {
max = mat[l][i];
ind = i;
}
}
return ind;
}
}
class test {
public static void main(String[] args) {
matrice M, N, R, Q;
Scanner S = new Scanner(System.in);
System.out.println("donner le nombre de lignes de M : ");
int L = S.nextInt();
System.out.println("donner le nombre de colonnes de M : ");
int C = S.nextInt();
M = new matrice(L,C);
M.remplissage();
System.out.println("donner le nombre de lignes de N");
L = S.nextInt();
System.out.println("donner le nombres de colonnes N");
C = S.nextInt();
N = new matrice(L,C);
N.remplissage();
R = M.somme(N);
if (R != null)
R.affichgeParligne();
Q = M.produit(N);
if (Q != null)
Q.affichgeParligne();
M.modif1(1, 1);
M.affichgeParligne();
N.modif2(0, 1, 1);
N.affichgeParligne();
System.out.println("l'indice du max du ligne 1 du matrice M est "
+ M.getindice(0));
}
} |
Partager