#include <stdio.
h>
#include <math.h>
#define SIZE 3
void afficher_matrice(float mat[SIZE][SIZE + 1]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE + 1; j++) {
printf("%8.2f ", mat[i][j]);
}
printf("\n");
}
}
void pivot_de_gauss(float mat[SIZE][SIZE + 1], float solution[SIZE]) {
// Réduction en forme triangulaire
for (int i = 0; i < SIZE; i++) {
// Trouver le pivot et effectuer l'échange de lignes si nécessaire
int pivot = i;
for (int j = i + 1; j < SIZE; j++) {
if (fabs(mat[j][i]) > fabs(mat[pivot][i])) {
pivot = j;
}
}
if (pivot != i) {
for (int j = 0; j < SIZE + 1; j++) {
float temp = mat[i][j];
mat[i][j] = mat[pivot][j];
mat[pivot][j] = temp;
}
}
// Vérifier que le pivot est non nul
if (fabs(mat[i][i]) < 1e-6) {
printf("Le système n'a pas de solution unique.\n");
return;
}
// Elimination des termes en-dessous du pivot
for (int j = i + 1; j < SIZE; j++) {
float ratio = mat[j][i] / mat[i][i];
for (int k = i; k < SIZE + 1; k++) {
mat[j][k] -= ratio * mat[i][k];
}
}
}
// Résolution par substitution arrière
for (int i = SIZE - 1; i >= 0; i--) {
solution[i] = mat[i][SIZE];
for (int j = i + 1; j < SIZE; j++) {
solution[i] -= mat[i][j] * solution[j];
}
solution[i] /= mat[i][i];
}
}
int main() {
float mat[SIZE][SIZE + 1], solution[SIZE];
// Lecture de la matrice augmentée
printf("Entrez les coefficients du système (3 équations, 3 inconnues) :\n");
for (int i = 0; i < SIZE; i++) {
printf("Équation %d :\n", i + 1);
for (int j = 0; j < SIZE + 1; j++) {
if (j < SIZE) {
printf("Coefficient a[%d][%d] : ", i + 1, j + 1);
} else {
printf("Terme constant b[%d] : ", i + 1);
}
scanf("%f", &mat[i][j]);
}
}
// Afficher la matrice augmentée initiale
printf("\nMatrice augmentée initiale :\n");
afficher_matrice(mat);
// Résolution avec la méthode du pivot de Gauss
pivot_de_gauss(mat, solution);
// Affichage des résultats
printf("\nSolutions du système :\n");
for (int i = 0; i < SIZE; i++) {
printf("x%d = %.6f\n", i + 1, solution[i]);
}
return 0;
}