Ejemplo de trabajo en c++ con matrices, definimos una constante de tipo entera Max con el valor cuatro, esa constante nos sirve para definir el tamaño de las dimensiones de la matriz.
Median el símbolo “[]” establecemos una dimensión de la la matriz, solamente necesitamos un bucle para hacer el recorrido por la misma. Solemos utilizar un bucle for debido a que conocemos el número de iteraciones de antemano “el mismo que longitud de la dimension”, y mediante los iteradores ir accediendo a cada posición de la matriz
Cuando tenemos un array estático se crea mediante los símbolos “{ }”;
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | using namespace std; #include <iostream> int const Max = 4 ; void rellenar ( int matriz [ Max ] [ Max ] ) ; void mostrar ( int matriz [ Max ] [ Max ] ); void rellenar ( int matriz [ Max ] [ Max ] ) { int auxiliar; for ( int i = 0 ; i < Max ; i ++ ) { for ( int j = 0 ; j < Max ; j++ ) { cout << " introduce el elmemento " << i << j << endl; cin >> auxiliar; matriz [ i ] [ j ] = auxiliar; } } } void mostrar ( int matriz [ Max ] [ Max ] ) { for ( int j = 0 ; j < Max ; j ++) { cout << "| "; for ( int z = 0 ; z < Max ; z ++) { if ( z < Max - 1) cout << matriz [j ] [ z ] << " , " ; else cout << matriz [ j ] [ z ]; } cout << " | " << endl; } } int main ( ) { int matriz [ Max] [ Max ]; int matriz2[ Max ] [ Max ] = { { 1 , 2 , 3 , 4} , { 2 , 3 , 4 , 5} , { 4 , 2, 3, 1 }, { 1 , 2 , 5, 4 } } ; rellenar ( matriz ); mostrar (matriz ); cout << "\n segunda matriz " << endl; mostrar ( matriz2); } |
