#include <iostream.h> #include <stdlib.h>
int operacion(int [][100], int [][100], int [][100], int , int , int, int, int); . int main() { int n,f,c,r,f1,f2,c1,c2, m1[100][100], m2[100][100], mr[100][100]; . cout<<"1 -- Sumar"<<endl<<"2 -- Restar"<<endl<<"Otro numero -- Salir"<<endl; cin>>n; while ((n>0) && (n<3)) { . f1=0; c1=0; f2=0; c2=0; while ((f1<1) || (f1>101)) { cout<<"Introduce las filas de la matriz 1 [1-100]: "; cin>>f1; } while ((c1<1) || (c1>101)) { cout<<"Introduce las columnas de la matriz 1 [1-100]: "; cin>>c1; } for (f=0; f<=f1; f++) { . for (c=0; c<=c1; c++) { cout<<"m1["<<f<<"]["<<c<<"] = "; cin>>m1[f][c]; } . } while ((f2<1) || (f2>101)) { cout<<endl<<"Introduce las filas de la matriz 2 [1-100]: "; cin>>f2; } while ((c2<1) || (c2>101)) { cout<<"Introduce las columnas de la matriz 2 [1-100]: "; cin>>c2; } for (f=0; f<=f2; f++) . { . for (c=0; c<=c2; c++) { cout<<"m2["<<f<<"]["<<c<<"] = "; cin>>m2[f][c]; } . } r=operacion(m1,m2,mr,n,f1,f2,c1,c2); . if (r==-1) cout<<endl<<"No se pudo realizar la operacion ya que el tamaño de las matrices no coincide"; . else { for (f=0; f<=f2; f++) . { . for (c=0; c<=c2; c++) . cout<<" "<<mr[f][c]<<" "; . cout<<endl; . } . } . cout<<endl<<"1 -- Sumar"<<endl<<"2 -- Restar"<<endl<<"Otro numero -- Salir"<<endl; cin>>n; } cout<<endl<<"Fin de programa..."<<endl; . system("PAUSE"); . return 0; } . int operacion(int m1[][100], int m2[][100], int mr[][100], int n, int f1, int f2,int c1, int c2) { . int f,c; . if ((f1==f2) && (c1==c2)) //si si se pueden sumar/restar . { . if (n==1) //suma . { for (f=0; f<=f1; f++) . { . for (c=0; c<=c1; c++) . mr[f][c] = m1[f][c] + m2[f][c]; . } } else //resta . { for (f=0; f<=f1; f++) { for (c=0; c<=c1; c++) . mr[f][c] = m1[f][c] - m2[f][c]; . } . } return 1; . } . else . {return -1;} . }
|