5.matrices Bidimensionales
5.matrices Bidimensionales
5.matrices Bidimensionales
using System.Collections.Generic; using System.Text; namespace ConsoleApplication155 { class Program { static void Main(string[] args) { int[,] mat1; int n,x,y;
mat1 = new int[n, n]; Console.WriteLine("ingrese valores de la matriz"); Console.WriteLine(""); for (x = 0; x < n; x++) { for (y = 0; y < n; y++) { Console.WriteLine("ingrese valores para la posicion {0} , {1}", x, y); mat1[x, y] = int.Parse(Console.ReadLine()); } } Console.WriteLine("la matriz es;"); Console.WriteLine("==============="); Console.WriteLine(""); for (x = 0; x < n; x++) { for (y = 0; y < n; y++) { Console.Write("{0}\t", mat1[x, y]); } Console.WriteLine(""); } } } } }
{ class Program { static void Main(string[] args) { //declaramos los arrays int[,] mat1; int[,] mat2; int[,] suma; //declaramos las variables int filas, columnas, i, j; //ingresamos la cantidad de filas y columnas de la matriz Console.WriteLine("Cuntas filas tendr la la matriz"); filas=int.Parse(Console.ReadLine()); Console.WriteLine("Cuntas columnas tendr la matriz"); columnas=int.Parse(Console.ReadLine()); //definimos el tamao del array mat1 = new int[filas, columnas]; mat2 = new int[filas, columnas]; suma=new int[filas,columnas]; //ingresamos los valores de la primera matriz Console.WriteLine("Ingrese valores de la matriz N 1"); Console.WriteLine(""); for (i = 0; i < filas; i++) { for (j = 0; j < columnas; j++) { Console.WriteLine("Ingrese valor para la posicin {0},{1}",i,j); mat1[i,j]=int.Parse(Console.ReadLine()); } } //Ingresamos los valores de la segunda matriz Console.WriteLine("Ingrese valores para la matriz N2"); Console.WriteLine(""); for (i = 0; i < filas; i++) { for (j = 0; j < columnas; j++) { Console.WriteLine("Ingrese valor para la posicin {0},{1}", i, j); mat2[i, j] = int.Parse(Console.ReadLine()); } } //suma de las matrices for (i = 0; i < filas; i++) { for (j = 0; j < columnas; j++) { suma[i, j] = mat1[i, j] + mat2[i, j]; } }
//impresion en pantalla de la suma Console.WriteLine("La suma de las matrices es: "); Console.WriteLine(" "); for (i = 0; i < filas; i++) { for (j = 0; j < columnas; j++) { Console.Write("{0}\t",suma[i,j]); } Console.WriteLine(""); } } } }
{1}:
m[i,j]=int.Parse(Console.ReadLine()); } } Console.WriteLine(); for (i = 0; i < nfilas; i++) { sumafila = 0; for (j = 0; j < ncol; j++) sumafila=sumafila+m[i, j]; Console.WriteLine("Suma de la fila{0}",sumafila); } } } }
3.TRANSPUESTA
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication78 { class Program { static void Main(string[] args) { int[,] mat1; int filas, columnas, x, y; Console.WriteLine("cuantas filas?"); filas = int.Parse(Console.ReadLine()); Console.WriteLine("cuantas columnas?"); columnas = int.Parse(Console.ReadLine()); { mat1 = new int[filas, columnas]; Console.WriteLine("ingrese valores de la matriz"); Console.WriteLine(""); for (x = 0; x < filas; x++) { for (y = 0; y < columnas; y++) { Console.WriteLine("ingrese valores para la posicion {0} , {1}", x, y); mat1[x, y] = int.Parse(Console.ReadLine()); } } Console.WriteLine("la matriz es;"); Console.WriteLine("==============="); Console.WriteLine("");
for (x = 0; x < filas; x++) { for (y = 0; y < columnas; y++) { Console.Write("{0}\t", mat1[x, y]); } Console.WriteLine(""); } { Console.WriteLine("la matriz traspuesta es:"); Console.WriteLine("======================="); Console.WriteLine(""); for (y = 0; y < columnas; y++) { for (x = 0; x < filas; x++) { Console.Write("{0} \t", mat1[x, y]); } { Console.WriteLine(""); } } } } } } }
} }
5.MATRIZ IDENTIDAD
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication138 { class Program { static void Main(string[] args) { int n; Console.WriteLine("Ingrese el orden de la matriz:"); n = int.Parse(Console.ReadLine()); int[,] matriz = new int[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) { matriz[i, j] = 1; } else { matriz[i, j] = 0; } } }
for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write("{0} ", matriz[i, j]); } Console.WriteLine(); } Console.ReadLine(); } }
de
de
de
de
de
System.Console.WriteLine("\nLas calificaciones de alumno son:\n\n"); for (j = 0; j < numa; j++) { d = (table[j, 0] + table[j, 1] + table[j, 2] + table[j, 3] + table[j, 4] + table[j, 5]) / 6;//Exception??? table[j, 6] = d; System.Console.WriteLine("Del alumano {0}\n", j + 1); System.Console.WriteLine("Espaol : {0}", table[j, 0]); System.Console.WriteLine("Matematicas : {0}", table[j, 1]); System.Console.WriteLine("Fisica : {0}", table[j, 2]); System.Console.WriteLine("Quimica : {0}", table[j, 3]); System.Console.WriteLine("Geografia : {0}", table[j, 4]); System.Console.WriteLine("Historia : {0}", table[j, 5]); if (table[j, 6] <= 6) { System.Console.WriteLine("\nEl alumno reprobo con un promedio de: {0}", table[j, 6]); } else if (table[j, 6] >= 7) { System.Console.WriteLine("\nEl alumno aprobo con un promedio de: {0}", table[j, 6]); } } }
} }
byte opcion; do { opcion = menu(); } while (opcion >= 5); switch (opcion) { case 1: // SUMA // Leemos el nmero de filas y columnas de las matrices 1 y 2 Console.WriteLine("Introduzca el nmero de filas de las matrices 1 y 2"); f1 = int.Parse(Console.ReadLine()); Console.WriteLine("Introduzca el nmero de columnas de las matrices 1 y 2"); c1 = int.Parse(Console.ReadLine()); // Pedimos los datos de filas y columnas Console.WriteLine("Introduzca los datos de la matriz 1 enumerandolos por filas:"); mat1 = leer(f1, c1); Console.WriteLine("Introduzca los datos de la matriz 2 enumerandolos por filas:"); mat2 = leer(f1, c1); //Mostramos la suma de ambas matrices suma(mat1, mat2); break; case 2: // RESTA matrices 1 y 2 // Leemos el nmero de filas y columnas de las
Console.WriteLine("Introduzca el nmero de filas de las matrices 1 y 2"); f1 = int.Parse(Console.ReadLine()); Console.WriteLine("Introduzca el nmero de columnas de las matrices 1 y 2"); c1 = int.Parse(Console.ReadLine()); // Pedimos los datos de filas y columnas Console.WriteLine("Introduzca los datos de la matriz 1 enumerandolos por filas:"); mat1 = leer(f1, c1); Console.WriteLine("Introduzca los datos de la matriz 2 enumerandolos por filas:"); mat2 = leer(f1, c1); // Mostramos la resta de ambas matrices resta(mat1, mat2); break; case 3: // PRODUCTO POR UN ESCALAR matriz 1 de la matriz 1"); // Leemos el nmero de filas y columnas de la Console.WriteLine("Introduzca el nmero de filas
f1 = int.Parse(Console.ReadLine()); Console.WriteLine("Introduzca el nmero de columnas de la matriz 1"); c1 = int.Parse(Console.ReadLine()); float escalar; Console.WriteLine("Introduzca el escalar por el que quiere multiplicar la matriz"); escalar = float.Parse(Console.ReadLine()); // Pedimos los datos de filas y columnas Console.WriteLine("Introduzca los datos de la matriz 1 enumerandolos por filas:"); mat1 = leer(f1, c1); // Mostramos la solucin prodEscalar(mat1, escalar); break; } Console.ReadKey(); } // Funcin que muestra el menu de seleccin de operaciones public static byte menu() { try { byte opcion; Console.SetCursorPosition(10, 1); Console.WriteLine("Men:"); Console.SetCursorPosition(0, 3); Console.WriteLine("Elija la operacin que quiere hacer:"); Console.WriteLine("1 - Suma de matrices"); Console.WriteLine("2 - Resta de matrices"); Console.WriteLine("3 - Producto por un escalar"); opcion = byte.Parse(Console.ReadLine()); if (opcion >= 1 && opcion <= 3) { Console.Clear(); return opcion; } else { Console.Clear(); return 5; }
// Funcin que lee los datos de las matrices public static float[,] leer(int filas, int columnas) {
float[,] ret = new float[filas, columnas]; for (int fila = 0; fila < filas; fila++) { for (int columna = 0; columna < columnas; columna++) { ret[fila, columna] = float.Parse(Console.ReadLine()); } } return ret; } // La funcin suma public static void suma(float[,] mat1, float[,] mat2) { Console.WriteLine("La suma de sus dos matrices es (enumeradas por filas)"); for (int fila = 0; fila <= mat2.GetUpperBound(0); fila++) { for (int columna = 0; columna <= mat2.GetUpperBound(1); columna++) { float suma; suma = mat1[fila, columna] + mat2[fila, columna]; Console.WriteLine(suma.ToString()); } Console.WriteLine(""); } } // La funcin resta public static void resta(float[,] mat1, float[,] mat2) { Console.WriteLine("La resta de sus dos matrices es (enumeradas por filas)"); for (int fila = 0; fila <= mat2.GetUpperBound(0); fila++) { for (int columna = 0; columna <= mat2.GetUpperBound(1); columna++) { float resta; resta = mat1[fila, columna] - mat2[fila, columna]; Console.WriteLine(resta.ToString()); } Console.WriteLine(""); } } // Producto por un escalar public static void prodEscalar(float[,] mat1, float escalar) { Console.WriteLine("La multiplicacin del escalar por su matriz es (enumerada por filas)"); for (int fila = 0; fila <= mat1.GetUpperBound(0); fila++) { for (int columna = 0; columna <= mat1.GetUpperBound(1); columna++) { float esc; esc = mat1[fila, columna] * escalar; Console.WriteLine(esc.ToString());
} } } }
} Console.WriteLine("");
{ mat1 = new int[2, 2]; Console.WriteLine("ingrese valores de la matriz 2*2"); Console.WriteLine(""); for (x = 0; x < 2; x++) { for (y = 0; y < 2; y++) { Console.WriteLine("ingrese valores para la posicion {0} , {1}", x, y); mat1[x, y] = int.Parse(Console.ReadLine()); } } Console.WriteLine("la matriz es;"); Console.WriteLine("==============="); Console.WriteLine(""); for (x = 0; x < 2; x++) { for (y = 0; y < 2; y++) { Console.Write("{0}\t", mat1[x, y]); } Console.WriteLine(""); } for (x = 0; x < 2; x++) { for (y = 0; y < 2; y++) {
mat1[x, y] = (mat1[0, 0]) * (mat1[1, 1]) (mat1[1, 0]) *( mat1[0, 1]); } } Console.WriteLine("Su determinante es:"); Console.WriteLine("=================="); Console.WriteLine(""); Console.Write("{0}\t", mat1[0, 0]);
} } }
if (a < filas) { for (y = 0; y < columnas; y++) { a = 0; sumfilas = mat1[a, y]; sumfilas = sumfilas + mat1[a, y]; } a = a + 1; } if (b < columnas) { for (x = 0; x < filas; x++) { b = 0; sumcolumnas = mat1[x, b]; sumcolumnas = sumcolumnas + mat1[x, b]; } b = b + 1; } //restando las sumas total = sumfilas - sumcolumnas; //impresion Console.WriteLine("LA OPERACION ES IGUAL A:"); Console.WriteLine("======================="); Console.WriteLine(""); Console.WriteLine("{0}\t", total); } } }
double[,] invA; int[,] I; //declaramos las variables int grado, x, y; //ingresamos el grado de la matris cuadrada Console.Write("\nINGRESE EL GRADO DE LA MATRIZ CUADRADA :"); grado = int.Parse(Console.ReadLine()); // definimos el tamao del array A = new double[grado, grado]; invA = new double[grado, grado]; I = new int[grado, grado]; // ingreso los valores de la matriz A * \n"); Console.WriteLine("\n * Ingrese los elementos de la Matriz for (x = 0; x < grado; x++) { for (y = 0; y < grado; y++) { Console.Write("\tIngrese el elemento [{0},{1}] : } A[x, y] = double.Parse(Console.ReadLine());
", x, y);
} Console.Write("\n"); //definimos la matriz indentidad I for (x = 0; x < grado; x++) { for (y = 0; y < grado; y++) { if (x == y) { I[x, y] = 1; } else { I[x, y] = 0; } } }
// Calcular la matriz inversa Console.WriteLine("\n * La matriz inversa es *\n"); for (x = 0; x < grado; x++) { for (y = 0; y < grado; y++) { invA[x, y] = I[x, y] / A[x, y]; Console.Write("\t{0,10:f4}", invA[x, y]);
} } } }
} Console.Write("\n");
for (x = 0; x < 3; x++) { for (y = 0; y < 3; y++) { Console.Write("{0}\t", ARREGLO1[x, y]); } Console.WriteLine(""); } c = ARREGLO1[0, 0] + ARREGLO1[1, 1] + ARREGLO1[2, 2]; d = ARREGLO1[2, 0] + ARREGLO1[1, 1] + ARREGLO1[0, 2]; e = c - d; Console.WriteLine("la operacion es:{0}", e); }
traspuesta[x, 1] = mat1[0, x]; } for (x = 0; x < fila; x++) { for (y = 0; y < columna; y++) { traspuesta[x, 2] = mat1[2, x]; } } Console.WriteLine("las matris traspuesta es "); Console.WriteLine("======================== :"); Console.WriteLine(""); for (x = 0; x < fila; x++) { for (y = 0; y < columna; y++) { Console.Write("{0}\t", traspuesta[x, y]); } Console.WriteLine(""); } } } } }