ARRAYS
ARREGLO BIDIMENSIONAL
Una matriz es una estructura de datos que permite almacenar un CONJUNTO de datos
del MISMO tipo.
Con un único nombre se define la matriz y por medio de DOS subíndices hacemos
referencia a cada elemento de la misma (componente).
Las filas y columnas comienzan a numerarse a partir de cero, similar a los vectores.
La forma general de declaración:
type[ , ] arrayName;
Una matriz bidimensional tiene la forma siguiente:
a11 a12 ............ a1n
a21 a22 ............ a2n
.............................
am1 am2 ............ amn
Para acceder al dato aij se hace de la manera siguiente:
c = a[i,j];
En el grafico tenemos una matriz de 3 filas y 5 columnas. Para hacer referencia a cada
elemento debemos indicar primero la fila y luego la columna, por ejemplo, en el componente
[1,4] se almacena el valor 97. En este ejemplo almacenamos valores enteros. Todos los
elementos de la matriz deben ser del mismo tipo (int, float, string etc.).
Ejemplo:
1. Crear una matriz de 3 filas por 5 columnas con elementos de tipo int, cargar sus
componentes y luego imprimirlas.
Definición de la clase Matriz1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Matriz1
{
class Matriz1
{
private int[,] mat;
public void Cargar()
{
mat=new int[3,5];
for(int f = 0;f < 3;f++)
{
for(int c = 0;c < 5;c++)
{
Console.Write("Ingrese componente:");
string linea;
linea = Console.ReadLine();
mat[f,c]=int.Parse(linea);
}
}
}
public void Imprimir()
{
for(int f = 0;f < 3;f++)
{
for(int c = 0;c < 5;c++)
{
Console.Write(mat[f,c]+" ");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
La clase Program que sirve para probar la clase Matriz1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Matriz1
{
class Program
{
static void Main(string[] args)
{
Matriz1 objMatriz = new Matriz1();
objMatriz.Cargar();
objMatriz.Imprimir();
Console.Read();
}
}
}
2.- Crear y cargar una matriz de 3 filas por 4 columnas. Imprimir la primera fila. Imprimir la
última fila e imprimir la primera columna.
Definición de la clase Matriz2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Matriz2
{
class Matriz2
{
private int[,] mat;
public void Cargar()
{
mat=new int[3,4];
for(int f = 0; f < 3; f++)
{
for(int c = 0; c < 4; c++)
{
Console.Write("Ingrese componente:");
string linea;
linea = Console.ReadLine();
mat[f,c]=int.Parse(linea);
}
}
}
public void PrimerFila()
{
Console.WriteLine("Primer fila de la matriz:");
for(int c = 0; c < 4; c++)
{
Console.WriteLine(mat[0,c]);
}
}
public void UltimaFila()
{
Console.WriteLine("Ultima fila de la matriz:");
for(int c = 0; c < 4; c++)
{
Console.WriteLine(mat[2,c]);
}
}
public void PrimerColumna()
{
Console.WriteLine("Primer columna:");
for(int f = 0; f < 3; f++)
{
Console.WriteLine(mat[f,0]);
}
}
}
}
La clase Program que sirve para probar la clase Matriz2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Matriz2
{
class Program
{
static void Main(string[] args)
{
Matriz2 objMatriz = new Matriz2();
objMatriz.Cargar();
objMatriz.PrimerFila();
objMatriz.UltimaFila();
objMatriz.PrimerColumna();
Console.Read();
}
}
}
3. Crear una matriz de 2 filas y 5 columnas. Realizar la carga de componentes por columna
(es decir primero ingresar toda la primera columna, luego la segunda columna y así
sucesivamente). Imprimir luego la matriz.
Definición de la clase Matriz3:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Matriz3
{
class Matriz3
{
private int[,] mat;
public void Cargar()
{
mat=new int[2,5];
Console.WriteLine("Carga de la matriz por columna:");
for(int c = 0; c < 5; c++)
{
for(int f = 0; f < 2; f++)
{
Console.Write("Ingrese componente " + " de la fila " + f + " y
la columna "+ c + " :");
string linea;
linea = Console.ReadLine();
mat[f,c]=int.Parse(linea);
}
}
}
public void Imprimir()
{
for(int f = 0; f < 2; f++)
{
for(int c = 0; c < 5; c++)
{
Console.Write(mat[f,c]+" ");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
La clase Program que sirve para probar la clase Matriz3:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Matriz3
{
class Program
{
static void Main(string[] args)
{
Matriz3 objMatriz = new Matriz3();
objMatriz.Cargar();
objMatriz.Imprimir();
Console.Read();
}
}
}
EJERCICIOS PROPUESTOS
1. Crear y cargar una matriz de 4 filas por 4 columnas. Imprimir la diagonal principal.
x - - -
- x - -
- - x -
- - - x
2. Se desea controlar los resultados de los alumnos en las diferentes asignaturas. El
programa debe leer las notas obtenidas de las distintas asignaturas y visualizarlas en la
pantalla en número, nombre, calificaciones y promedio del estudiante. Visualizar el
promedio general por cada asignatura. Mostrarla en una interfaz gráfica de usuario.