Está en la página 1de 4

//Multiplicacion de matrices P=AxB

#include<iostream>
using namespace std;
int main()
{
int fA,cA,fB,cB,i,j,k;
float A[10][10], B[10][10],P[10][10];
//LECTURA DE DATOS PARA LA MATRIZ A
cout<<"\n\t Ing. el nro de filas de la matriz A: ";cin>>fA;
cout<<"\n\t Ing. el nro de columnas de la matriz A: ";cin>>cA;
cout<<"\t Ingrese los elementos de la matriz A:"<<endl;
for(i=0;i<fA;i=i+1)
{
for(j=0;j<cA;j=j+1)
{
cout<<"\t A["<<i+1<<","<<j+1<<"]=";cin>>A[i][j];
}
}
//LECTURA DE DATOS PARA LA MATRIZ B
cout<<"\n\t Ing. el nro de filas de la matriz B: ";cin>>fB;
cout<<"\n\t Ing. el nro de columnas de la matriz B: ";cin>>cB;
cout<<"\t Ingrese los elementos de la matriz B:"<<endl;
for(i=0;i<fB;i=i+1)
{
for(j=0;j<cB;j=j+1)
{
cout<<"\t B["<<i+1<<","<<j+1<<"]=";cin>>B[i][j];
}
}
if(fB==cA)
{//si es verdadero se puede muliplicar
for(i=0;i<fA;i=i+1)
{
for(j=0;j<cB;j=j+1)
{
P[i][j]=0;
for(k=0;k<cA;k=k+1)
{
P[i][j]=P[i][j]+A[i][k]*B[k][j];
}
}
}
//mostrar la matriz producto
cout<<"\n\tMATRIZ PRODUCTO"<<endl;
for(i=0;i<fA;i=i+1)
{
cout<<"\t";
for(j=0;j<cB;j=j+1)
{
cout<<P[i][j]<<" ";
}
cout<<endl;
}
}
else
{ cout<<"\tNO SE PUEDE MULTIPLICAR"<<endl;
}
}

//Multiplicacion de matrices P=AxB


// usando matrices con parametros de referencia
#include<iostream>
using namespace std;
const int maxc=10;
//LECTURA DE DATOS DE UNA MATRIZ
void LeeMat(float A[][maxc],int &x,int &y)
{
int i,j;
cout<<"\n\t Ing. el nro de filas de la matriz: ";cin>>x;
cout<<"\n\t Ing. el nro de columnas de la matriz: ";cin>>y;
cout<<"\t Ingrese los elementos de la matriz:"<<endl;
for(i=0;i<x;i=i+1)
{
for(j=0;j<y;j=j+1)
{
cout<<"\t A["<<i+1<<","<<j+1<<"]=";cin>>A[i][j];
}
}
}
int main()
{
int fA,cA,fB,cB,i,j,k;
float A[10][10], B[10][10],P[10][10];
LeeMat(A,fA,cA);
LeeMat(B,fB,cB);
if(fB==cA)
{//si es verdadero se puede muliplicar
for(i=0;i<fA;i=i+1)
{
for(j=0;j<cB;j=j+1)
{
P[i][j]=0;
for(k=0;k<cA;k=k+1)
{
P[i][j]=P[i][j]+A[i][k]*B[k][j];
}
}
}
//mostrar la matriz producto
cout<<"\n\tMATRIZ PRODUCTO"<<endl;
for(i=0;i<fA;i=i+1)
{
cout<<"\t";
for(j=0;j<cB;j=j+1)
{
cout<<P[i][j]<<" ";
}
cout<<endl;
}
}
else
{ cout<<"\tNO SE PUEDE MULTIPLICAR"<<endl;
}
}

//Multiplicacion de matrices P=AxB


//los encabezados al inicio, las declaraciones al final
#include<iostream>
using namespace std;
const int maxc=10;
//LECTURA DE DATOS DE UNA MATRIZ
void LeeMat(float A[][maxc],int &x,int &y);
int main()
{
int fA,cA,fB,cB,i,j,k;
float A[10][10], B[10][10],P[10][10];
LeeMat(A,fA,cA);
LeeMat(B,fB,cB);
if(fB==cA)
{//si es verdadero se puede muliplicar
for(i=0;i<fA;i=i+1)
{
for(j=0;j<cB;j=j+1)
{
P[i][j]=0;
for(k=0;k<cA;k=k+1)
{
P[i][j]=P[i][j]+A[i][k]*B[k][j];
}
}
}
//mostrar la matriz producto
cout<<"\n\tMATRIZ PRODUCTO"<<endl;
for(i=0;i<fA;i=i+1)
{
cout<<"\t";
for(j=0;j<cB;j=j+1)
{
cout<<P[i][j]<<" ";
}
cout<<endl;
}
}
else
{ cout<<"\tNO SE PUEDE MULTIPLICAR"<<endl;
}
}
void LeeMat(float A[][maxc],int &x,int &y)
{
int i,j;
cout<<"\n\t Ing. el nro de filas de la matriz: ";cin>>x;
cout<<"\n\t Ing. el nro de columnas de la matriz: ";cin>>y;
cout<<"\t Ingrese los elementos de la matriz:"<<endl;
for(i=0;i<x;i=i+1)
{
for(j=0;j<y;j=j+1)
{
cout<<"\t A["<<i+1<<","<<j+1<<"]=";cin>>A[i][j];
}
}
}

También podría gustarte