24 - Matrices irregulares o dentadas |
C# nos permite crear matrices irregulares o dentadas. Se dice que una matriz es irregular si la cantidad de elementos de cada fila varía. Luego podemos imaginar una matriz irregular:

Como podemos ver la fila cero tiene reservado dos espacios, la fila uno reserva cuatro espacios y la última fila reserva espacio para tres componentes.
La sintaxis para declarar una matriz irregular es:
int [][] mat;
Primero creamos la cantidad de filas dejando vacío el espacio que indica la cantidad de columnas:
mat=new int[3][];
Luego debemos ir creando cada fila de la matriz indicando la cantidad de elementos de la respectiva fila:
mat[0]=new int[2]; mat[1]=new int[4]; mat[2]=new int[3];
Luego la forma para acceder a sus componentes debe ser utilizando corchetes abiertos y cerrados para cada índice:
mat[0][0]=120;
Dará un error si queremos cargar la tercer componente de la fila cero (esto debido a que no existe):
mat[0][2]=230;
Luego si queremos saber la cantidad de filas que tiene la matriz:
Console.Write(mat.Length);
Si queremos saber la cantidad de elementos de una determinada fila:
Console.Write("Cantidad de elementos de la fila 0:"+mat[0].Length);
Console.Write("Cantidad de elementos de la fila 1:"+mat[1].Length);
Console.Write("Cantidad de elementos de la fila 2:"+mat[2].Length);
Confeccionaremos un programa que permita crear una matriz irregular y luego imprimir la matriz en forma completa.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MatrizIrregular1
{
class MatrizIrregular1
{
private int[][] mat;
public void Cargar()
{
Console.Write("Cuantas fila tiene la matriz:");
string linea=Console.ReadLine();
int filas=int.Parse(linea);
mat=new int[filas][];
for(int f = 0; f < mat.Length; f++)
{
Console.Write("Cuantas elementos tiene la fila " + f + ":");
linea = Console.ReadLine();
int elementos=int.Parse(linea);
mat[f]=new int[elementos];
for(int c = 0; c < mat[f].Length; c++)
{
Console.Write("Ingrese componente:");
linea=Console.ReadLine();
mat[f][c]=int.Parse(linea);
}
}
}
public void Imprimir()
{
for(int f = 0; f < mat.Length; f++)
{
for(int c = 0; c < mat[f].Length; c++)
{
Console.Write(mat[f][c]+" ");
}
Console.WriteLine();
}
Console.ReadLine();
}
static void Main(string[] args)
{
MatrizIrregular1 ma = new MatrizIrregular1();
ma.Cargar();
ma.Imprimir();
}
}
}
Primero creamos la cantidad de filas que tendrá la matriz (en los corchetes para las columnas no disponemos valor):
Console.Write("Cuantas fila tiene la matriz:");
string linea=Console.ReadLine();
int filas=int.Parse(linea);
mat=new int[filas][];
Dentro del primer for pedimos que ingrese la cantidad de elementos que tendrá cada fila y utilizamos el operador new nuevamente, pero en este caso se están creando cada fila de la matriz (C# trata a cada fila como un vector):
Console.Write("Cuantas elementos tiene la fila " + f + ":");
linea = Console.ReadLine();
int elementos=int.Parse(linea);
mat[f]=new int[elementos];
Dentro del for interno hacemos la carga de las componentes propiamente dicho de la matriz (podemos ir cargando cada fila a medida que las vamos creando):
for(int c=0;c < mat[f].Length;c++)
{
Console.Write("Ingrese componente:");
linea=Console.ReadLine();
mat[f][c]=int.Parse(linea);
}
Luego imprimimos la matriz en forma completa teniendo cuidado las condiciones que disponemos en cada for.
El primer for se repite tantas veces como filas tiene la matriz: f<mat.Length y
el for interno se repite tantas veces como elementos tiene la fila que estamos procesando c<mat [f].Length:
for(int f = 0; f < mat.Length; f++)
{
for(int c = 0; c < mat[f].Length; c++)
{
Console.Write(mat[f][c]+" ");
}
Console.WriteLine();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MatrizIrregular2
{
class MatrizIrregular2
{
private int[][] mat;
public void Cargar()
{
mat=new int[5][];
for(int f = 0; f < mat.Length; f++)
{
mat[f]=new int[f+1];
for(int c = 0; c < mat[f].Length; c++)
{
Console.Write("Ingrese componente:");
string linea = Console.ReadLine();
mat[f][c]=int.Parse(linea);
}
}
}
public void Imprimir()
{
for(int f = 0; f < mat.Length; f++)
{
for(int c = 0; c < mat[f].Length; c++)
{
Console.Write(mat[f][c]+" ");
}
Console.WriteLine();
}
Console.ReadKey();
}
static void Main(string[] args)
{
MatrizIrregular2 ma = new MatrizIrregular2();
ma.Cargar();
ma.Imprimir();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MatrizIrregular3
{
class MatrizIrregular3
{
private string[] nombres;
private int[][] dias;
public void Cargar()
{
nombres=new string[3];
dias=new int[3][];
for(int f = 0; f < nombres.Length; f++)
{
Console.Write("Ingrese el nombre del empleado:");
nombres[f]=Console.ReadLine();
Console.Write("Cuantas días faltó el empleado:");
string linea = Console.ReadLine();
int faltas=int.Parse(linea);
dias[f]=new int[faltas];
for(int c = 0; c < dias[f].Length; c++)
{
Console.Write("Ingrese nro de día:");
linea = Console.ReadLine();
dias[f][c]=int.Parse(linea);
}
}
}
public void Inasistencias()
{
for(int f = 0; f < nombres.Length; f++)
{
Console.WriteLine(nombres[f] + " faltó " + dias[f].Length + " días");
}
}
public void EmpleadoMensosFaltas()
{
int faltas=dias[0].Length;
string nom=nombres[0];
for(int f = 1; f < dias.Length; f++)
{
if (dias[f].Length < faltas)
{
faltas=dias[f].Length;
nom=nombres[f];
}
}
Console.WriteLine("El empleado que faltó menos es "+nom+" con "+faltas+" faltas.");
Console.ReadKey();
}
static void Main(string[] args)
{
MatrizIrregular3 ma = new MatrizIrregular3();
ma.Cargar();
ma.Inasistencias();
ma.EmpleadoMensosFaltas();
}
}
}