exercice 17 // solutions d'un systeme d'équations linéaires

exercice 17: 

ecrire un programme qui trouve les solutions d'un systeme d'equations linéaires,par exemple:  

  x   + y  + 2z  =1
 2x  + y  + z    =2
  x   + 2y+ 3z  =3

programme 17:


#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{        
         /* decleretion des varaibles*/

      int i,l,n,k,j,u,q; 
      float max,c,z; 
      /* la declaration et la saisi de la matrice*/

      printf(" donnez l'ordre de la matrice:\n");
      scanf("%d", &n); 
      float tab[n][n];
      float b[n];
      for (i=0;i<n;i++){
      for (j=0;j<n;j++){
      printf(" donner le coefficient [%d][%d]:\n",i,j);
      scanf("%f", &tab[i][j]);
      }
      }
      printf("remplissez le vecteur b\n");
      for(i=0;i<n;i++){
           scanf("%f",&b[i]);
           }            
      /* l'affichage de la matrice*/
      printf(" votre systeme est:\n");
          for (i=0;i<n;i++) {
            for (j=0;j<n;j++)  {
            printf(" %f ", tab[i][j]);
             }
             printf(" =%f",b[i]);
            printf("\n");
            }
for(j=0;j<n;j++){
                 max=tab[0][j];
                 /* calcul du max des colonnes*/
                 for(l=j;l<n;l++){
                                  if(fabs(tab[l][j])>max){
                                                    max=tab[l][j];
                                                    u=l;
                                                    }
                                                    }
/* permutation des lignes*/
                if(u!=j){
                         for(k=j;k<n;k++){
                                          c=tab[j][k];
                                          tab[j][k]=tab[u][k];
                                          tab[u][k]=c;
                                          c=b[j];
                                          b[j]=b[u];
                                          b[u]=c;
                                          }
                                          }
                /* trigonaliser la matrice */
                 for(i=j+1;i<n;i++){
                                    z=-1*(tab[i][j]/tab[j][j]);
                                    for(q=j;q<n;q++){
                                                     tab[i][q]=tab[j][q]*z+tab[i][q];
                                                     }
                                                     b[i]=z*b[j]+b[i];  
                                                     }
                                                     }
/* on va diagonaliser la matrice*/
   for(j=n-1;j>0;j--){
                      for(i=j-1;i>=0;i--){
                                          z=-(tab[i][j]/tab[j][j]);
                                          tab[i][j]+=z*tab[j][j];
                                          b[i]+=z*b[j];
                                          }
                                          }
          for(i=0;i<n;i++){
                             b[i]=b[i]/tab[i][i];
                             tab[i][i]=1;
                             }
                                                     
                      /* l'affichage du systeme resultat*/
      printf("\n le systeme resultat est:\n");
          for (i=0;i<n;i++) {
            for (j=0;j<n;j++)  {
            printf(" %f ", tab[i][j]);
             }
             printf(" =%f",b[i]);
            printf("\n");
            }  
            getch();  
            }


Aucune note. Soyez le premier à attribuer une note !