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
|
#include <cstdlib>
#include <iostream>
using namespace std;
void AfficherMatrice(float **M, int n)
{ //affiche une matrice de taille n*n
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
{
cout<< M[i][j] << "\t" ;
}
cout<< endl;
}
}
void AfficherVecteur (float *y, int n)
{ //affiche un vecteur de taille n
for (int i=0; i<n; i++)
{
cout<< y[i] << "\t";
}
cout<<endl;
}
void MTI(float **M, int n)
{ // matrice triangulaire inférieure
for(int i=0; i<n; i++)
{
for (int j=0; j<=i; j++)
{
cout<<M[i][j]<< "\t";
}
cout<<endl;
}
}
float Somme(float **M, float *x, int i, int j)
{ // M.x
float S=0;
//Toujours pas d'index 0 ? for (i=1; i<j;i++)
{
S+=M[j][i]*x[i];
}
return S;
//Retourner quelque chose, je ne sais pas
//si c'est S simplement;}
void SystMatTI (float **M, float *b, float *x, int n)
{ //trouver x tel que Mx=b avec M tiangulaire inférieure
//Pourquoi jamais index 0 ?
x[1]=b[1]/M[1][1];
//!!! j<n !!! les plantages viennent de là !
//Là, tu déborde de b, de la matrice, etc... des zones non allouées !!
for (int j=2; j<n; j++)
{
x[j]=(b[j]- Somme(M,x,1,j-1))/M[j][j];
//Est tu sûr que M[j][j] != 0 ?
//Si jamais = 0 ça va planter
//du genre "a cesser de fonctionner..."
//A toi de prévoir le cas...
}
}
int main(int argc, char *argv[])
{
int n;
cout << "entrer la valeur de n" <<endl;
cin>>n;
float** A = new float* [n]; //Pas besoin de la macro NULL
for (int i=0; i<n; i++)
{
A[i]= new float[n];
//Problème : tu veux que A soit une vrai matrice (n*n), mais
//tu n'alloue qu'une matrice triangulaire
}
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
{
cout<< "Entrer A" <<i+1<<","<<j+1<<endl;
cin>> A[i][j];
}
}
AfficherMatrice(A,n);
cout<<endl;
MTI(A,n);
//float *x = new float [n];
//Pourquoi pas :
float x[n];
//float *b = new float [n];
//Idem
float b[n];
for (int i=0; i<n; i++)
{
cout<< "Entrer b" <<i+1<< endl;
cin>>b[i];
}
AfficherVecteur(b,n);
cout<<endl;
SystMatTI(A,b,x,n);
cout<<endl;
//Afficher le résultat peut-être ?
AfficherVecteur(x,n);
for (int i=0; i<n; i++)
{
delete[] A[i];
}
delete[] A;
delete[] b;
delete[] x;
//OK, les desalloc sont bonnes
//x=NULL; : Inutile...
system("PAUSE");
return EXIT_SUCCESS;
} |
Partager