begin process at 2008 07 06 12:54:57
1 205 545 membres
122 nouveaux aujourd'hui
14 119 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

MANIPULATION DE MATRICES CARRÉES POUR DEBUTANTS!


Information sur la source

Catégorie :Maths & Algorithmes Niveau : Débutant Date de création : 15/12/2004 Date de mise à jour : 15/12/2004 00:28:12 Vu : 5 743

Note :
Aucune note

Commentaire sur cette source (3)
Ajouter un commentaire et/ou une note

Description

Permet de manipuler des matrices carrées(addition,transposée,produit) Je suis debutant en c++ et je veux juste apporter ma modeste contribution en attendant d'évoluer... Merci

Source

  • // Programmes de manipulation d'opérations entre matrices carrées
  • #include<iostream>
  • using namespace std;
  • class matrice {
  • private:
  • int m,n;
  • double M[16][16];
  • public:
  • matrice(unsigned int,unsigned int);
  • int getcols() const; //nbre de lignes;
  • int getlignes() const; //nbre de colonnes
  • void lire();
  • void ecrire()const;
  • void setlignescols();
  • void transposer(matrice T);
  • void add_matr(matrice a1,matrice a2);
  • void prod_matr(matrice p1,matrice p2);
  • };
  • matrice::matrice(unsigned int line=0,unsigned int col=0){
  • m=line;
  • n=col;
  • }
  • int matrice::getlignes()const {
  • return m;
  • }
  • int matrice::getcols()const {
  • return n;}
  • void matrice::lire(){
  • for(int i=0;i < getlignes();i++)
  • {
  • for(int j=0;j < getcols();j++)
  • {
  • cout<< "M(" <<i << "," << j << ")=";
  • cin>>M[i][j];
  • }
  • }
  • }
  • void matrice::ecrire() const{
  • for(int i=0;i < getlignes();i++)
  • {
  • for(int j=0;j < getcols();j++)
  • {
  • cout<< M[i][j]<<" ";
  • }
  • cout<<endl<<endl;
  • }
  • }
  • void matrice::add_matr(matrice a1,matrice a2) {
  • if(a1.getlignes() != a2.getcols()){
  • cout<<"somme impossible car matrices incompatibles"; }
  • else {
  • matrice C(a1.getlignes(),a2.getcols());
  • for( int i=0;i < getlignes();i++)
  • {
  • for(int j=0;j < getcols();j++)
  • C.M[i][j]= a1.M[i][j]+ a2.M[i][j];
  • }
  • C.ecrire();
  • }
  • }
  • void matrice::prod_matr(matrice p1,matrice p2) {
  • matrice P(p1.getlignes(),p2.getcols());
  • if( p1.getlignes() != p2.getcols() )
  • {
  • cout<<"Matrices incompatibles,operation impossible"<<endl;
  • }
  • else {
  • for(int i=0;i < getlignes();i++)
  • {
  • for(int j=0;j < p1.getcols();j++)
  • {
  • P.M[i][j]=0;
  • for(int k=0;k <p2.getcols();k++)
  • {
  • P.M[i][j] += p1.M[i][k]*p2.M[k][j] ;
  • }
  • }
  • }
  • }
  • P.ecrire();
  • }
  • void matrice::transposer(matrice a) {
  • matrice T(a.getlignes(),a.getcols());
  • for(int i=0;i < getlignes();++i)
  • {
  • for(int j=0;j<getcols();++j)
  • {
  • T.M[i][j]=a.M[j][i];
  • }
  • }
  • T.ecrire();
  • }
  • void main() {
  • matrice a(3,3),b(3,3),s(3,3),t(3,3),p(3,3);
  • char choix;
  • cout<<"entrer les elements de la premiere matrice :";cout<<endl;
  • a.lire();
  • a.ecrire();
  • cout<<"entrer les elements de la seconde matrice";cout<<endl;
  • b.lire();
  • b.ecrire();
  • cout<<"entrez votre choix : addition = 1 ,transposee = 2 multiplication = 3 : "; cin>>choix; cout <<endl;
  • if(choix=='1'){
  • cout<<"somme des deux matrices precedentes: "<<endl<<endl;
  • s.add_matr(a,b);
  • }
  • else
  • {
  • if(choix=='2'){
  • cout<<"la transposee de la première matrice est:"<<endl<<endl;
  • t.transposer(a);
  • }
  • else
  • {
  • if(choix=='3'){
  • cout<<"produit des matrices" <<endl<<endl;
  • p.prod_matr(a,b);
  • }
  • else
  • {
  • cout<<"vous avez choisie une operation non definie :";
  • }
  • }
  • }
  • }
// Programmes de manipulation d'opérations entre matrices carrées

#include<iostream>
using namespace std;

class matrice {
private:
int m,n;
double M[16][16];

public:
matrice(unsigned int,unsigned int);
int getcols() const;           //nbre de lignes;
int getlignes() const;     //nbre de colonnes
void lire();
void ecrire()const;
void setlignescols();
void transposer(matrice T);
void  add_matr(matrice a1,matrice a2);
void  prod_matr(matrice p1,matrice p2);
};
matrice::matrice(unsigned int line=0,unsigned int col=0){
m=line;
n=col;
}

int matrice::getlignes()const {
return m;
}
int matrice::getcols()const {
 return n;}
void matrice::lire(){
	
	for(int i=0;i < getlignes();i++)
	{
		for(int j=0;j < getcols();j++)
		{
		  cout<<	"M(" <<i << "," << j  << ")=";
		    cin>>M[i][j];
		}
	}
}
void matrice::ecrire() const{
	for(int i=0;i < getlignes();i++)
	{
		for(int j=0;j < getcols();j++)
		{
			
		  cout<< M[i][j]<<" ";
		}
		cout<<endl<<endl;
	}


}
void   matrice::add_matr(matrice a1,matrice a2) {
  if(a1.getlignes() != a2.getcols()){ 
    cout<<"somme impossible car matrices incompatibles"; }
  else { 
 
  matrice C(a1.getlignes(),a2.getcols());  
  for( int i=0;i < getlignes();i++)
	{
		for(int j=0;j < getcols();j++)
		
			C.M[i][j]= a1.M[i][j]+ a2.M[i][j];
		
	}
  
   C.ecrire();
  }
}

void  matrice::prod_matr(matrice p1,matrice p2) {
	matrice P(p1.getlignes(),p2.getcols());
	if( p1.getlignes() != p2.getcols() ) 
	{
		cout<<"Matrices incompatibles,operation impossible"<<endl;
	} 
	else {
 for(int i=0;i < getlignes();i++)
	{
		for(int j=0;j < p1.getcols();j++)
		{      
		    P.M[i][j]=0;
		    for(int k=0;k <p2.getcols();k++)
			{
		 P.M[i][j] += p1.M[i][k]*p2.M[k][j] ;
			}
		}
 }
	}
		P.ecrire();
}

void matrice::transposer(matrice a) {
matrice T(a.getlignes(),a.getcols());
	for(int i=0;i < getlignes();++i)
	{
		for(int j=0;j<getcols();++j)
		{
			T.M[i][j]=a.M[j][i];
		}
	}

	T.ecrire();
}

void main() {

matrice a(3,3),b(3,3),s(3,3),t(3,3),p(3,3);
char choix;
cout<<"entrer les elements de la premiere matrice :";cout<<endl;
a.lire();
a.ecrire();
cout<<"entrer les elements de la seconde matrice";cout<<endl;
b.lire();
b.ecrire();
cout<<"entrez votre choix : addition = 1 ,transposee = 2 multiplication = 3 : "; cin>>choix; cout <<endl;

if(choix=='1'){
cout<<"somme des deux matrices precedentes: "<<endl<<endl;
s.add_matr(a,b);
}
else
{
	if(choix=='2'){
cout<<"la transposee de la première matrice est:"<<endl<<endl;
t.transposer(a);
	}
else
{
	if(choix=='3'){
cout<<"produit des matrices" <<endl<<endl;
p.prod_matr(a,b);
	}
else
{ 
cout<<"vous avez choisie une operation non definie :";

}

}
}
}

Conclusion

On peut bien entendu le modifier pour qu'il puisse calculer des matrices de dimensions m*n  quelconques!!!
15 décembre 2004 00:28:12 :
juste des arrangement syntaxiques...
  • signaler à un administrateur
    Commentaire de ymca2003 le 15/12/2004 17:02:36

    Lorsque tu passe tes matrices en paramètres, passe les en référence (const si pas modifiées dans la fct) :

    void     matrice::add_matr(const matrice& a1, const matrice& a2)
    {
    }

    => cela évitera de recopier sur la pile les matrices à chaque appel

  • signaler à un administrateur
    Commentaire de pmbala le 17/12/2004 09:28:38

    Merci ymca2003,je tiendrais compte de ton conseil,je te remercie.Un jr peut que je serai un vrai Crack (LoL)

  • signaler à un administrateur
    Commentaire de zbigzo le 19/01/2005 18:15:34

    tu es vraiment le plus je sais pas moi

Ajouter un commentaire

Pub



Appels d'offres

Plugin Dialer outlook
Budget : 2 000€
Travail graphique- ill...
Budget : 1 000€
creation de marque et ...
Budget : 1 000€

CalendriCode

Juillet 2008
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Boutique

Boutique de goodies CodeS-SourceS