Autor Tema: C# Ejemplo Suma de Matrices con Try Catch cargar arreglo bidimensional y sumarlo  (Leído 4433 veces)

Gabrield Marquez

  • Sin experiencia
  • *
  • APR2.COM
  • Mensajes: 15
    • Ver Perfil
Código del ejemplo:

Código: [Seleccionar]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace try_catch_1
{
    public class Matriz
    {
        private int cantFila;
        private int cantColumnas;

        private int[,] matriz;

        public Matriz(int nf, int nc)
        {
            cantFila = nf;
            cantColumnas = nc;
            matriz = new int[nf, nc];
        }

        public void CargarMatriz()
        {
            for (int i = 0; i < this.cantFila; ++i) {
                for (int j = 0; j < this.cantColumnas; ++j) {
                    bool hayError = true;

                    while (hayError) {
                        Console.Write(" Valor (  {0},  {1}) : ", i, j);
                        try
                        {
                            int valor = int.Parse(Console.ReadLine());
                            matriz[i, j] = valor;
                            hayError = false;
                        }
                        catch (Exception e) {
                            Console.WriteLine("Valor incorrecto,  verficar......");
                        }
                    }
                }
               

                }
            }   

        public int GetCantidadFilas()
        {
            return this.matriz.GetLength (0);
        }   

        public int GetCantidadColumnas ()
        {
            return this.matriz.GetLength (1);
        }   

        public static Matriz Sumar (Matriz matrizA, Matriz matrizB)
        {
            int cfA = matrizA.GetCantidadFilas ();
            int ccA = matrizA.GetCantidadColumnas ();

            int cfB = matrizB.GetCantidadFilas ();
            int ccB = matrizB.GetCantidadColumnas ();

            if ((cfA != cfB) || (ccA != ccB))   
            {
                throw new Exception ("Matrices incompatibles para la Suma.");
            }   

            Matriz r = new Matriz (cfA, ccA);

            for (int i = 0; i < cfA; ++i)   
            {
                for (int j = 0; j < ccA; ++j)   
                {
                    r.GetMatriz () [i,j] = matrizA.GetMatriz () [i,j] + matrizB.GetMatriz () [i,j];
                }
            }   

            return r;
        }   

        public static Matriz operator + (Matriz matrizA, Matriz matrizB)   
        {
            return Sumar (matrizA, matrizB);
        }   

        public int[,] GetMatriz ()
        {
            return this.matriz;
        }
        }
    }
« Última modificación: 07 de Enero 2016, 15:24 por Ogramar »

 

Sobre la educación, sólo puedo decir que es el tema más importante en el que nosotros, como pueblo, debemos involucrarnos.

Abraham Lincoln (1808-1865) Presidente estadounidense.

aprenderaprogramar.com: Desde 2006 comprometidos con la didáctica y divulgación de la programación

Preguntas y respuestas

¿Cómo establecer o cambiar la imagen asociada (avatar) de usuario?
  1. Inicia sesión con tu nombre de usuario y contraseña.
  2. Pulsa en perfil --> perfil del foro
  3. Elige la imagen personalizada que quieras usar. Puedes escogerla de una galería de imágenes o subirla desde tu ordenador.
  4. En la parte final de la página pulsa el botón "cambiar perfil".