MATRICES
Una matriz es un vector de vectores o un también llamado array bidimensional. La manera de declarar una matriz es C++ es similar a un vector:
int es el tipo de dato, matrix es el nombre del todo el conjunto de datos y debo de especificar el numero de filas y columnas.
Las matrices también pueden ser de distintos tipos de datos como char, float, double, etc. Las matrices en C++ se almacenan al igual que los vectores en posiciones consecutivas de memoria.
Usualmente uno se hace la idea que una matriz es como un tablero, pero internamente el manejo es como su definición lo indica, un vector de vectores, es decir, los vectores están uno detrás del otro juntos.
La forma de acceder a los elementos de la matriz es utilizando su nombre e indicando los 2 subíndices que van en los corchetes.
No olvidar que tanto filas como columnas se enumeran a partir de 0. Bueno y para recorrer una matriz podemos usar igualmente un bucle. En este caso usando 2 for:
#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
No hay comentarios:
Publicar un comentario