Advertisement
mrevilca31

TP 1 Punto 3 (2018)

Sep 10th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace TP1Punto3
  8. {
  9.     class Program
  10.     {
  11.         static void cargarMatriz(int[,] mat)
  12.         {
  13.             Console.Clear();
  14.             for (int j = 0; j < mat.GetLength(1); j++)
  15.             {
  16.                 Console.Clear();
  17.                 Console.WriteLine("Fila {0}" , j+1 );
  18.                 for (int i = 0; i < mat.GetLength(0); i++)
  19.                 {
  20.                     mat[i, j] = int.Parse(Console.ReadLine());
  21.                 }
  22.             }
  23.         }
  24.  
  25.         static void mostrarMatriz(int[,] mat)
  26.         {
  27.             Console.Clear();
  28.             Console.WriteLine("Matriz: ");
  29.             Console.WriteLine("");
  30.             for (int j = 0; j < mat.GetLength(1); j++)
  31.             {
  32.                 for (int i = 0; i < mat.GetLength(0); i++)
  33.                 {
  34.                     Console.Write("\t" + mat[i, j]);
  35.                 }
  36.                 Console.WriteLine("");
  37.             }
  38.         }
  39.  
  40.         static void sumaDiagonal(int[,] mat)
  41.         {
  42.             int suma = 0;
  43.             for (int i = 0; i < mat.GetLength(0); i++)
  44.             {
  45.                 bool aux = true;
  46.                 int j = 0;
  47.                 while (aux)
  48.                 {
  49.                     if (i==j)
  50.                     {
  51.                         suma = suma + mat[i, j];
  52.                         aux = false;
  53.                     }
  54.                     else
  55.                     {
  56.                         j = j + 1;
  57.                     }
  58.                 }
  59.             }
  60.             Console.WriteLine("\nLa suma de la diagonal principal es: " + suma);
  61.             Console.WriteLine("\n\nPresione una tecla para continuar..");
  62.             Console.ReadKey();
  63.         }
  64.  
  65.         static void mensaje(ref int resp)
  66.         {
  67.             Console.Clear();
  68.             Console.WriteLine("Elija una opcion");
  69.             Console.WriteLine("\n1-Nueva matriz.");
  70.             Console.WriteLine("\n2-Salir.");
  71.             resp = int.Parse(Console.ReadLine());
  72.         }
  73.  
  74.         static void Main(string[] args)
  75.         {
  76.             int resp = 0;
  77.             do
  78.             {
  79.                 Console.Clear();
  80.                 Console.WriteLine("Ingrese la dimension de la matriz..");
  81.                 int dim = int.Parse(Console.ReadLine());
  82.                 int[,] matriz = new int[dim, dim];
  83.                 cargarMatriz(matriz);
  84.                 mostrarMatriz(matriz);
  85.                 sumaDiagonal(matriz);
  86.                 mensaje(ref resp);
  87.             } while (resp!=2);
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement