Trouver une ressource (Nouvelle version du moteur, plus rapide & pertinent, essayez le !)
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
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!!!
Historique
- 15 décembre 2004 00:28:12 :
- juste des arrangement syntaxiques...
Sources de la même categorie
Commentaires
|
CalendriCode
| | | L | M | M | J | V | S | D |
| | 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 | | | |
|
|