• WORD
  • FORMATO AUTOMATICO Y ESTILOS
  • INSERTAR CUADRO DE TEXTO Y ECUACION
  • REFERENCIASTABLA DE CONTENIDO Y BIBLIOGRAFIA
  • Ir a Prácticas
  • EXCEL
  • FORMULAS I y II
  • TABLAS Y GRAFICOS DINAMICOS
  • SUBTOTALES Y BASE DE DATOS
  • MACROS
  • Ir a Prácticas
  • AUTOCAD
  • 2D:DIBUJO Y ACOTAR
  • INSERTAR BLOQUES Y PLOT
  • 3D: MODELADO Y EDICION DE SOLIDOS
  • RENDER Y ANIMACIONES
  • Ir a Prácticas

viernes, 6 de febrero de 2026

C#: MATRICES

 ARREGLOS(MATRICES)


INTRODUCIR UNA MATRIZ:

using System;

 

class Program

{

    static void Main()

    {

                Console.Write("¿Cuántas FILAS? ");

        int filas = int.Parse(Console.ReadLine());

 

        Console.Write("¿Cuántas COLUMNAS? ");

        int columnas = int.Parse(Console.ReadLine());

 

       

        int[,] matriz = new int[filas, columnas];

 

        Console.WriteLine("\n--- INGRESAR DATOS ---");

        for (int i = 0; i < matriz.GetLength(0); i++)  // GetLength(0) = FILAS

        {

            for (int j = 0; j < matriz.GetLength(1); j++)  // GetLength(1) = COLUMNAS

            {

                Console.Write($"matriz[{i},{j}] = ");

                matriz[i, j] = int.Parse(Console.ReadLine());

            }

        }

 

        // 4. MOSTRAR matriz

        Console.WriteLine("\n--- MATRIZ LLENA ---");

        for (int i = 0; i < matriz.GetLength(0); i++)

        {

            for (int j = 0; j < matriz.GetLength(1); j++)

            {

                Console.Write($"{matriz[i, j],4}");

            }

            Console.WriteLine();

        }

    }

}

 

 

BUSQUEDA DE MATRICES DESORDENADAS CON BUSQUEDA ,LINEAL

using System;

 

class Program

{

    public static int[] BuscarLineal(int[,] matriz, int valor)

    {

        int filas = matriz.GetLength(0);

        int columnas = matriz.GetLength(1);

 

        // Recorrer TODA la matriz elemento por elemento

        for (int i = 0; i < filas; i++)

        {

            for (int j = 0; j < columnas; j++)

            {

                if (matriz[i, j] == valor)

                {

                    return new int[] { i, j };  // ¡Encontrado!

                }

            }

        }

        return new int[] { -1, -1 };  // No encontrado

    }

 

    static void ImprimirMatriz(int[,] matriz)

    {

        int filas = matriz.GetLength(0);

        for (int i = 0; i < filas; i++)

        {

            for (int j = 0; j < matriz.GetLength(1); j++)

                Console.Write($"{matriz[i, j],4}");

            Console.WriteLine();

        }

    }

 

 

 

    static void Main()

    {

        // Matriz DESORDENADA

        int[,] matrizDesordenada = {

            { 5, 1, 9 },

            { 3, 8, 2 },

            { 7, 4, 6 }

        };

 

        Console.WriteLine("Matriz DESORDENADA:");

        ImprimirMatriz(matrizDesordenada);

        Console.WriteLine();

 

        // Probar búsquedas

        int[] resultado1 = BuscarLineal(matrizDesordenada, 8);

        Console.WriteLine($"Buscar 8 → [{resultado1[0]}, {resultado1[1]}]");  // [1,1]

 

        int[] resultado2 = BuscarLineal(matrizDesordenada, 10);

        Console.WriteLine($"Buscar 10 → [{resultado2[0]}, {resultado2[1]}]"); // [-1,-1]

    }

}

Ordenar una matriz conviertiendola en un array

using System;

 

class Program

{

    static void Main()

    {

        // Matriz DESORDENADA inicial

        int[,] matriz = {

            { 5, 1, 7 },

            { 3, 8, 2 },

            { 7, 4, 6 }

        };

 

        Console.WriteLine("MATRIZ ORIGINAL:");

        ImprimirMatriz(matriz);

 

        // ORDENAR TODA LA MATRIZ

        OrdenarMatriz(matriz);

 

        Console.WriteLine("\nMATRIZ ORDENADA:");

        ImprimirMatriz(matriz);

    }

 

   

    static void OrdenarMatriz(int[,] matriz)

    {

        int filas = matriz.GetLength(0);

        int cols = matriz.GetLength(1);

 

       

        int[] temp = new int[filas * cols];

        int index = 0;

        for (int i = 0; i < filas; i++)

            for (int j = 0; j < cols; j++)

                temp[index++] = matriz[i, j];

 

        Array.Sort(temp);

 

       

        index = 0;

        for (int i = 0; i < filas; i++)

            for (int j = 0; j < cols; j++)

                matriz[i, j] = temp[index++];

    }

 

    static void ImprimirMatriz(int[,] matriz)

    {

        for (int i = 0; i < matriz.GetLength(0); i++)

        {

            for (int j = 0; j < matriz.GetLength(1); j++)

                Console.Write($"{matriz[i, j],4}");

            Console.WriteLine();

        }

    }

}

 

Busqueda de elementos en matrices

Método GetLength(0)->filas

Método GetLength(1)->columnas

 

 

using System;

 

class Program

{

    // Método estático debe estar dentro de la clase

    public static int[] BuscarEsquinas(int[,] matriz, int valor)

    {

        int fila = 0;

        int col = matriz.GetLength(1) - 1;

 

        while (fila < matriz.GetLength(0) && col >= 0)

        {

            if (matriz[fila, col] == valor)

            {

                return new int[] { fila, col };

            }

            else if (valor < matriz[fila, col])

            {

                col--;  // Descartar columna

            }

            else

            {

                fila++;  // Descartar fila

            }

        }

        return new int[] { -1, -1 };

    }

 

    static void Main()

    {

        // Uso para matriz ordenada:

        int[,] matrizOrdenada = {

            { 1, 4, 7 },

            { 2, 5, 8 },

            { 3, 6, 9 }

        };

 

        int[] resultado = BuscarEsquinas(matrizOrdenada, 5);

        Console.WriteLine($"Encontrado en [{resultado[0]}, {resultado[1]}]");  // [1, 1]

    }

}

 

No hay comentarios.:

Publicar un comentario