LABORATORIO 2

ESTRUCTURA DE UNA MATRIZ


//ESTRUCTURA 4 EN BLANCO CON FUNCIONES
// USO DE VECTORES
#include<iostream>
#include<math.h>
using namespace std;

// ZONA DE DECLARACION PUBLICA
const int m=3, n=3;
int i, j, sumatoria, Busca, YY, R;

int M[n][m]; // DECLARACION DE UNA MATRIZ

void LeerMatriz(); // DECLARACION FUNCIONES
void EscribirMatriz();
void Buscar(int YY);
int SumarElementos();

int main ()
{
 int opcion;
 do
 {      //INICIO DEL DO - WHILE
  cout<<"********* MENU DE UNA MATRIZ **********\n\n";
  cout<<" 1) LECTURA DE LA MATRIZ n*m \n";
  cout<<" 2) ESCRITURA DE LA MATRIZ n*m \n";
  cout<<" 3) ENCONTRAR EL ELEMENTO \n";  
  cout<<" 4) SUMAR ELEMENTOS EN LA MATRIZ \n\n";
  cout<<"       DIGITE <0> PARA SALIR \n";
  cout<<"       Elija una Opcion < > \n\n";
  cout<<"*************************************\n\n";
  cout<<"   ELIJA UNA OPCION : "; cin>>opcion;
    //2)ASIGNACION
    switch (opcion)
    { //INICIO DEL CASO 1
    case 1:
     {// CONTENIDO 1 (INICO)
       cout<<"******* LECTURA DE LA MATRIZ ******\n\n";
       LeerMatriz();
       cout<<"************************************\n\n";
    } //FIN DEL CASO 1
     break;    
  case 2:
     {//INICIO DEL CASO 2
       cout<<"******* ESCRITURA DE LA MATRIZ ******\n\n";
       EscribirMatriz();
       cout<<"*********************************\n\n";
     } //FIN DEL CASO 2
    break;

  case 3:
     {//INICIO DEL CASO 3
       cout<<"******* ENCONTRAR UN ELEMENTO DE LA MATRIZ ******\n\n";
       cout<<"Ingrese el e-nesimo elemento a Buscar: "; cin>> Busca;      
       Buscar(Busca);
       cout<<"*********************************\n\n";
     } //FIN DEL CASO 2
    break;

  case 4:
     {//INICIO DEL CASO 4
       cout<<"******* SUMAR ELEMENTOS EN LA MATRIZ ******\n\n";
       R = SumarElementos();
       cout<<"LA SUMA DE LOS ELEMENTOS DE LA MATRIZ ES:"<< R<< endl;
       cout<<"\n******************************************\n\n";
     } //FIN DEL CASO 2
    break;
 
   }// FIN DE SWITCH
 }while (opcion !=0); // FIN DEL DO - WHILE

  cout<<endl;cout<<"\n";
  system("pause");
  return 0;
} //FIN DEL PROGRAMA

// ZONA DE DESARROLLO DE FUNCIONES

void LeerMatriz()
{
  cout<<endl;  
  cout<<"LECTURA DE LA MATRIZ M "<<endl;  
  for(i=1; i<=n; i++)
    for(j=1; j<=m; j++)
     {
     cout<<"ingrese valor del elemento M["<<i<<"]["<<j<<"] = "; cin>>M[i][j];
     }      
  cout<<endl;
}

void EscribirMatriz()
{
  cout<<endl;  
  cout<<"ESCRITURA DE LA MATRIZ M "<<endl;  
  for(i=1; i<=n; i++)
    for(j=1; j<=m; j++)
     {
     cout<<"El elemento M["<<i<<"]["<<j<<"] = "<<M[i][j]<<endl;
     }      
  cout<<endl;
}

void Buscar(int YY)
{
 for(i=1; i<=n; i++)
  for(j=1; j<=m; j++)    
   {
     if (M[i][j]==YY)      
     cout<<" Encontrmos al elemento M["<<i<<"]["<<j<<"] = "<<M[i][j] <<endl;
   }      
} // FIN DE BUSCAR

int SumarElementos()
{
 sumatoria = 0;
 for(i=1; i<=n; i++)
  for(j=1; j<=m; j++)
   {
    sumatoria = sumatoria + M[i][j];
   }
   return sumatoria;
} // FIN DE SUMAR


 EJERCICIO  CON MATRIZ

1.- Escribir un programa que haga el producto de dos matrices 3x3. El programa debe incluir un procedimiento que lea las matrices, una función que haga el producto y otro procedimiento que escriba el resultado:

SOLUCIÓN
#include <cstdlib>
#include <iostream>
using namespace std;
void leermatriz (float m[3][3])
{
 int i,j;
 for (i=1;i<=3;i++)
 {
 for (j=1;j<=3;j++)
 {
 cout<<"introducir el elemento "<<i<<","<<j<<endl;
 cin>>m[i][j];
 }
 }
 }
void producto_matrices (float a [3][3],float b[3][3],float p[3][3])
{
 int i,j,k;
 for (i=1;i<=3;i++)
 {
 p[i][j]=0;
 for (j=1;j<=3;j++)
 {
 p[i][j]=p[i][j]+a[i][k]*b[k][j];
 }
 }
 }

void escribir_matriz (float m[3][3])
{
 int i,j;
 for (i=1;i<=3;i++)
 {
 for (j=1;j<=3;j++)
 {
 cout<<m[i][j]<<" ";
 }
 cout<<endl;
 }
 }
int main()
{
 float a[3][3];
 leermatriz (a);
 leermatriz (b);
 producto_matrices (a,b,p);
 escribir_matriz (p);

 system("PAUSE");
 return EXIT_SUCCESS;
}








No hay comentarios:

Publicar un comentario