jueves, 26 de noviembre de 2009

ARREGLOS EN PROGRAMACION DE C#

Un arreglo contiene variables a las cuales se accede a través de índices, todas las variables contenidas en el arreglo son referidos como elementos los cuales deben ser del mismo tipo, por lo que el tipo del arreglo.

Los arreglos en C# son referencias a objetos. Un arreglo value type no contiene instancias boxed.

El valor inicial de un arreglo es null, un arreglo de objetos es creado utilizando new.

Cuando un arreglo es creado inicialmente contiene los valores por default para los tipos que este contendrá.

Sintaxis:

tipo[] identificador;

Note que para definir un arreglo se utilizan los corchetes [] después del tipo del arreglo.

Ejemplo:

string[] aPersonas;

Es posible inicializar un arreglo al momento de crearlo:

string[] asPersonas = new string[] {"Tim Berners-Lee","Brendan Eich","Dennis Ritchie","James Gosling"};

Durante la inicialización es posible omitir new tipo[x] y el compilador podría determinar el tamaño de almacenamiento para el arreglo del número de items en la lista de inicialización:

string[] asPersonas = {"Tim Berners-Lee","Brendan Eich","Dennis Ritchie","James Gosling"};

Cada elemento de un arreglo de ints es un int con el valor 0:

int[] aiNumeros = new int[5];

Cada elemento de un arreglo de strings es un string con el valor null:

string[] asNombres = new string[5];


EJERCICIOS:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] palabras = new string[10];
for (int i = 0; i < 10; i++) // sirve para escribir n numeros
{
Console.Write("ingre el nombre {0} : ", i);
palabras [i]= Console.ReadLine();// imprime las palabras para poder escribir
}
string b; // agrego una variable b para para buscar la palabra
Console.Write("ingrese la palabra buscar : ");
int encontro = -1; // esto srive como bandera
int repetir = 0;
b = Console.ReadLine();
for (int j = 0; j < 10; j++) // sirve para buscar la palabra
{
if (palabras[j] == b)
{
encontro = j;// se encuentra la palabra y sies igual a j que esta entre
//los diez, reemplazsara hasta encontrar la palabra j
repetir++;// se repite para poner las veces que se repiten
}
}
if (encontro == -1) // coomo encontro es una absendara, dice que busqye a partir de -1 la varialbe y la escriba
{
Console.WriteLine("no se encontro lo buscado ");
}
else
{
Console.WriteLine("se encontro la palabra {0} en la poisicion {1} y se repite {2}",b,encontro,repetir);
}
Console.ReadLine();

}
}
}

---------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[,] matriz = { { 6, 7 }, { 8, 9 } };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
Console.Write(matriz[i, j]);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

---------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[,] matriz = { { 5,6, 7 }, { 8, 9,10 } };
for (int i = 0; i < 2; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 3; j++)// j: siempre van hacer las columnas
{
Console.Write(matriz[i , j] + " ");// el mas es un salto de linea
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

-------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[,] matriz = { { 5,15, 20,25}, {3,6,9,12 }, {8,16,24,32 } };
for (int i = 0; i < 3; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 4; j++)// j: siempre van hacer las columnas
{
Console.Write(matriz[i , j] + " ");// el mas es un saltyo de linea
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
----------------------------------------------
se pide los valores con los teclados

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[,] matriz = new int [3,4];
// para pedir por pantalla:
for (int i = 0; i < 3; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 4; j++)// j: siempre van hacer las columnas
{
Console.Write("matriz [{0},{1}]= ", i, j);// con el white imprime al costado y con el whiteline se imprime abajo
matriz[i, j] = Convert.ToInt16(Console.ReadLine());
}
}

// para imprimir
for (int i = 0; i < 3; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 4; j++)// j: siempre van hacer las columnas
{
Console.Write(matriz[i , j] + "\t");// el mas es un salto de linea y el \t son para q salga un poco mas ordenada la matriz
}
Console.WriteLine();// para que de un salo de linea
}
Console.ReadLine();
}
}
}

-----------------------------------------------------------------
para que imprima que salga la matriz ya impresa sin estar poniendo los valores con teclado:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random objeto = new Random();
int[,] matriz = new int [3,4];
// para pedir por pantalla:
for (int i = 0; i < 3; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 4; j++)// j: siempre van hacer las columnas
{
matriz[i, j] = objeto.Next(20);
}
}

// para imprimir
for (int i = 0; i < 3; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 4; j++)// j: siempre van hacer las columnas
{
Console.Write(matriz[i , j] + "\t");// el mas es un salto de linea y el \t son para q salga un poco mas ordenada la matriz
}
Console.WriteLine();// para que de un salo de linea
}


Console.ReadLine();
}
}
}


----------------------------------------------------------------------------------------------------
imprimir solo los pares:
\t=para espacio
\t=para saltar de un lugar a otro.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random objeto = new Random();
int[,] matriz = new int [3,4];
// para pedir por pantalla:
for (int i = 0; i < 3; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 4; j++)// j: siempre van hacer las columnas
{
matriz[i, j] = objeto.Next(20);
}
}

// para imprimir
for (int i = 0; i < 3; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 4; j++)// j: siempre van hacer las columnas
{
Console.Write(matriz[i , j] + "\t");// el mas es un salto de linea y el \t son para q salga un poco mas ordenada la matriz
}
Console.WriteLine();// para que de un salo de linea
}
int[] pares = new int[12];
int c = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
if (matriz[i, j] % 2 == 0)
{
pares[c] = matriz[i, j];
c++;
}
}
}
Console.WriteLine("======================================================");
for (int k = 0; k < c; k++)
Console.Write(pares[k] + "\t");
Console.WriteLine("\n=====================================================");

Console.ReadLine();
}
}
}

--------------------------------------------------------------------------------

hacer un arreglo que imprima los impares:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random objeto = new Random();
int[,] matriz = new int [3,4];
// para pedir por pantalla:
for (int i = 0; i < 3; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 4; j++)// j: siempre van hacer las columnas
{
matriz[i, j] = objeto.Next(20);
}
}

// para imprimir
for (int i = 0; i < 3; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 4; j++)// j: siempre van hacer las columnas
{
Console.Write(matriz[i , j] + "\t");// el mas es un salto de linea y el \t son para q salga un poco mas ordenada la matriz
}
Console.WriteLine();// para que de un salo de linea
}
int[] pares = new int[12];
int c = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
if (matriz[i, j] % 2 == 0)
{
pares[c] = matriz[i, j];
c++;
}
}
}
Console.WriteLine("======================================================");
for (int k = 0; k < c; k++)
Console.Write(pares[k] + "\t");
Console.WriteLine("\n=====================================================");

int[] impares = new int[12];
int a = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
if (matriz[i, j] % 2 != 0)
{
impares[a] = matriz[i, j];
a++;
}
}
}


Console.WriteLine("\n=====================================================");
Console.WriteLine("\t\nimpares");
for (int k = 0; k < a; k++)
Console.Write(impares[k] + "\t");
Console.WriteLine("\n=====================================================");



Console.ReadLine();
}
}
}

---------------------------------------------------------------------------------------------------------------------
leer los numeros de:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[10];

// int[] a = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
int total = 0;
for (int i = 0; i < a.Length; i++)
{
a[i] = Convert.ToInt16(Console.ReadLine());
total += a[i];
Console.WriteLine("suma parcial "+total);
}
Console.WriteLine("Suma de todos los elementos del array: " + total);
Console.ReadLine();
}
}

--------------------------el mismo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[10];

// int[] a = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
int total = 0;
for (int i = 0; i < 10; i++)
{
Console.WriteLine("ingrese el numero");
a[i] = Convert.ToInt16(Console.ReadLine());
total += a[i];
Console.WriteLine("suma parcial "+total);
}
Console.WriteLine("Suma de todos los elementos del array: " + total);
Console.ReadLine();
}
}
}

--------------------------------------------------------------------
ingresar notas:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[] notas = new int[10];
int[] frecuencia = new int[21];
for (int j = 0; j < notas.Length; j++)
{
Console.Write("Ingrese Nota==>");
notas[j] = Convert.ToInt16(Console.ReadLine());
frecuencia[notas[j]]++;
}
for (int i=0; i<21;i++)
Console.WriteLine( "la frecuencia de las nota {0} = {1}",i,frecuencia [i]);
Console.ReadLine();
}
}
}

----------------------------------------------------------------------------------------------------
arreglos ordenados:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class arregla50elementos
{
static void Main(string[] args)
{
Random objeto = new Random();
int []arreglo= new int[50];
for (int i = 0; i < 10; i++)
{
arreglo[i]=objeto.Next(100);
Console.WriteLine( arreglo[i]);
}
int temp;
for (int i = 0; i < 10; i++)
{

for (int j = 0; j < 10; j++)
{
// comprando parejas de numeros
if (arreglo[j] > arreglo[j + 1])
{
// asiganando valores ordenados.

temp = arreglo[j];
arreglo[j] = arreglo[j + 1];
arreglo[j + 1] = temp;

}
}
}

Console.WriteLine("arreglo ordenado");
for (int i = 0; i < 10; i++)
{

Console.WriteLine(arreglo[i]);
}
Console.ReadLine();
}
}
}

--------------------------------------------------------------------------
MULTIPLICACION:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{

int[,] a = new int[4, 4];
int[,] b = new int[4, 4];
int[,] c = new int[4, 4];

Random objeto = new Random();
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{

a[i, j] = objeto.Next(20);
}
}

for (int i = 0; i < 4; i++)//i es filas
{
for (int j = 0; j < 4; j++)// j es columna //ve columna
{
Console.Write(a[i, j] + "\t");//para q imprima la matriz

}
Console.WriteLine();//para que de un salto de linea
}
Console.WriteLine("===========================================");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{

b[i, j] = objeto.Next(20);
}
}
for (int i = 0; i < 4; i++)//i es filas
{
for (int j = 0; j < 4; j++)// j es columna //ve columna
{
Console.Write(b[i, j] + "\t");//para q imprima la matriz

}
Console.WriteLine();//para que de un salto de linea
}
Console.WriteLine("========================================================");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
c[i, j] = 0;
for (int k = 0; k < 4; k++)
{
c[i, j] = c[i, j] + (a[i, k] * b[k, j]);

}
}
}
for (int i = 0; i < 4; i++)//i es filas
{
for (int j = 0; j < 4; j++)// j es columna //ve columna
{
Console.Write(c[i, j] + "\t");
}
Console.WriteLine();
Console.ReadLine();

}
}
}
}


¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
MATRICES EN C#

DEFINICION

Una matriz tiene las propiedades siguientes:

Una matriz puede ser unidimensional, multidimensional o escalonada.

El valor predeterminado de los elementos numéricos de matriz se establece en cero y el de los elementos de referencia se establece en null.

Una matriz escalonada es una matriz de matrices y por consiguiente sus elementos son tipos de referencia y se inicializan en null.

Las matrices se indizan basadas en cero: una matriz con n elementos se indiza desde 0 hasta n-1.

Los elementos de una matriz pueden ser de cualquier tipo, incluido el tipo matriz.

Los tipos de matriz son tipos de referencia derivados del tipo base abstracto Array. Puesto que este tipo implementa IEnumerable e IEnumerable, puede utilizar la iteración foreach en todas las matrices de C#.


EJERCICIOS

para sacar de una matriz los numeros pares Y IMPARES.............
____________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace trabajo_1_arreglos_bide
{
class Program
{
static void Main(string[] args)
{
Random objeto = new Random();
int[,] matriz = new int[3, 4];
//ingresar
for (int i = 0; i < j =" 0;" i =" 0;" j =" 0;" pares =" new" c =" 0;" i =" 0;" j =" 0;" 2 ="="" k="0;" n ="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="" impares =" new" b =" 0;" i =" 0;" j =" 0;" k =" 0;" n ="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="" objeto =" new" matriz =" new" i =" 0;" j =" 0;" i =" 0;" j =" 0;" pares =" new" impares =" new" cp =" 0;" ci =" 0;" i =" 0;" j =" 0;" 2 ="="" k="0;" n ="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="" k =" 0;" n ="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="="" matriz =" new" i =" 0;" j =" 0;" i =" 0;" j =" 0;" matriz =" new" i =" 0;" j =" 0;" i =" 0;" j =" 0;" i =" 0;" j =" 0;">= i)
{
Console.Write(matriz[i, j] + "\t");
}
else
{
Console.Write("" + "\t");
}
}
Console.WriteLine("\n\n\n");//para que de un salto de linea
}


Console.ReadLine();

}
}
}
_____________________________________________________________________________________

de un cuadrado saca
****
* *
* *
****
________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace trabajo_1_arreglos_bide
{
class Program
{
static void Main(string[] args)
{

string[,] matriz = new string[4, 4];
//ingresar
for (int i = 0; i < j =" 0;" i =" 0;" j =" 0;" i =" 0;" j =" 0;" i="="0" i="="3" j="="0" j="="3)" matriz =" new" i =" 0;" j =" 0;" i =" 0;" j =" 0;" i =" 0;" j =" 0;" i="="2" i="="2" j="="2" j="="2)" matriz =" new" i =" 0;" j =" 0;" i =" 0;" j =" 0;" i =" 0;" j =" 0;" j ="="" i ="="" j="="1" j="="3)" i="="1" i="="3)))" matriz =" new" i =" 0;" j =" 0;" i =" 0;" j =" 0;" i =" 0;" j =" 0;" j ="="" i="="4))//" a =" 12;" b =" 14;" c =" suma(a," a="16;" b="78;" c="suma(a,b);//suma" c =" suma(100," f="1;" x =" Convert.ToInt16(Console.ReadLine());" i =" x;"> 1; i--)
{
f = f * i;
}
Console.WriteLine("EL FACTORIAL ES "+ f);
}
}
}
--------------------------------------------------------------

INGRESAR 10 NUMEROS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
public static int fac(int x)
{
int f = 1;
for (int i = x; i > 1; i--)
{
f = f * i;
}
return f;
}


static void Main(string[] args)
{
int x;
Console.Write("INGRESE EL NUMERO");
x = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("EL FACTORIAL ES {0}", fac(x));
}
}
}

-------------------------------------------------------------------------------

HACER PROGRAMA Q INGRESE 10 NUMEROS POR FACTORIAL:

using System.Text;

namespace ConsoleApplication2
{
class Program
{
public static int fac(int x)
{
int f = 1;
for (int i = x; i > 1; i--)
{
f = f * i;
}
return f;
}

static void Main(string[] args)
{
Random objeto = new Random();
int[] a = new int[10];
int[] b = new int[10];
for (int i = 0; i < es ="{1}" f =" 1;" i =" x;"> 1; i--)
{
f = f * i;
}
return f;
}
public static void imprimir(int[] a, string l)
{
for (int i = 0; i < objeto =" new" a =" new" b =" new" i =" 0;" es ="{1}" objeto =" new" i =" 0;" a =" new" b =" new" c =" new" i="0;i<10;i++)" x=" mostrar" y=" la" z="la" w="la" b="x" d="y" objeto =" new" i =" 0;" i =" 0;" a =" new" b =" new" c =" new" d =" new" e =" new" f =" new" g =" new" h =" new" w =" new" y=" new" i="0;i<10;i++)" i =" 0;" i =" 0;" i =" 0;" i =" 0;" style="color: rgb(255, 102, 102); font-style: italic; font-weight: bold;">RANDOM EN C# 03.11.09 trabajo de ordenar numeros --------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Random objeto = new Random(); int[] arreglo = new int[50]; for (int i = 0; i < i =" 0;" j =" 0;"> arreglo[j + 1])
{
temp = arreglo[j];
arreglo[j] = arreglo[j + 1];
arreglo[j + 1] = temp;
}
}
}
Console.Write("arreglo ordenado ");
for (int i = 0; i < a =" new" b =" new" c =" new" objeto =" new" i =" 0;" j =" 0;" i =" 0;" j =" 0;" i =" 0;" j =" 0;" i =" 0;" j =" 0;" i =" 0;" j =" 0;" k =" 0;" i =" 0;" j =" 0;" objeto =" new" arreglo =" new" i =" 0;" i =" 0;" j =" 0;"> arreglo[j + 1])
{
temp = arreglo[j];
arreglo[j] = arreglo[j + 1];
arreglo[j + 1] = temp;
}
}
}
Console.Write("arreglo ordenado ");
for (int i = 0; i < 10; i++) { Console.WriteLine(arreglo[i]); } Console.ReadLine(); } } } ___________________________________________________________________________________________ multiplicacion de matris ____________________________________________________________________________________________ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { int[,] a = new int[4, 4]; int[,] b = new int[4, 4]; int[,] c = new int[4,4]; Random objeto = new Random(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { a[i, j] = objeto.Next(20); } } for (int i = 0; i < 4; i++)//i es filas { for (int j = 0; j < 4; j++)// j es columna //ve columna { Console.Write(a[i, j] + "\t");//para q imprima la matriz } Console.WriteLine();//para que de un salto de linea } Console.WriteLine("==========================================="); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { b[i, j] = objeto.Next(20); } } for (int i = 0; i < 4; i++)//i es filas { for (int j = 0; j < 4; j++)// j es columna //ve columna { Console.Write(b[i, j] + "\t");//para q imprima la matriz } Console.WriteLine();//para que de un salto de linea } Console.WriteLine("========================================================"); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { c[i,j] = 0; for (int k = 0; k < 4; k++) { c[i,j] = c[i,j] + (a[i,k] * b[k,j]); } } } for (int i = 0; i < 4; i++)//i es filas { for (int j = 0; j < 4; j++)// j es columna //ve columna { Console.Write(c[i, j]+"\t"); } Console.WriteLine(); } } } } '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' SENTENCIA FOREACH EN PROGRAMACION DE C# La Sentencia foreach es un comando para enumerar los elementos de una colección. foreach(Tipo indentificador in expresión){} La variable de iteración es declarada por el Tipo, indentificador y expresión correspondiente a la colección. La variable de iteración representa el elemento de la colección para cada iteración. El siguiente ejemplo muestra el uso de for: using System; class App{ public static void Main(string[] aArgs){ for(int i = 0; i < aArgs.Length; i++){ Console.WriteLine("Elemento " + i + " = " + aArgs[i]); } } } El ejemplo anterior implementado con foreach: using System; class App{ public static void Main(string[] aArgs){ foreach(String s in aArgs){ Console.WriteLine(s); } } } No es posible asignar un nuevo valor a la variable de iteración. No se puede pasar la variable de iteración como un parámetro ref o out. Para que una clase soporte la sentencia foreach, la clase debe soportar un método con la firma GetEnumerator() y la estructura, clase o interface que regresa debe tener un método público MoveNext y una propiedad pública Current. En el siguiente ejemplo el método GetEnvironmentVariables() regresa una interfaz de tipo IDictionary. Es posible acceder a las colecciones Keys y Values de la interfaz IDictionary: using System; using System.Collections; class SentenciaForEach{ public static void Main(){ IDictionary VarsAmb = Environment.GetEnvironmentVariables(); Console.WriteLine("Existen {0} variables de ambiente declaradas", VarsAmb.Keys.Count); foreach(String strIterador in VarsAmb.Keys){ Console.WriteLine("{0} = {1}", strIterador, VarsAmb[strIterador].ToString()); } } } Nota, es necesario tener una precaución extra al decidir el tipo de variable de iteración, porque un tipo equivocado no puede ser detectado por el compilador, pero si detectado en tiempo de ejecución y causar una excepción. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
SENTENCIA IF EN PROGRAMACION DE C# DEFINICION:
Al escribir uno o varios flujos de acción el código contenido en estos se ejecutará siempre y cuando la evaluación de la expresión en la sentencia if se evalue como verdadera (tenga cuidado en C# if(0){} o if(1){} no es válido). if(expresión-booleana){la expresión se evaluo verdadera} Es posible indicar código alterno en caso de que la expresión booleana se evalue falsa: if(expresión-booleana){ la expresión se evaluo verdadera }else{ la expresión se evaluo falsa } Nota C# no puede convertir valores numéricos a booleanos, solo puede hacer comparaciones entre ellos para evaluar el resultado de la expresión el cual es un valor booleano. using System; class SeleccionIf{ public static void Main(){ if(1 == 1){ Console.WriteLine("se evaluo verdadero"); } /* No es soportado por C# if(0){ Console.WriteLine("?"); } */ } } Nota el operador de igualdad en C# es ==, si está habituado a otra forma, sera cosa tiempo acostumbrarse a escribirlo correctamente, en la siguiente tabla se muestran los operadores válidos en C#: Operador Evalua == Verdadero, si ambos valores son los mismos != Verdadero, si los valores son diferentes <, <=, >, >= Verdadero, si el valor cumple con la condición

Los operadores de la tabla son implementados via la sobrecarga de operadores y la implementación es especifica para el tipo de dato, si se comparan dos variables de diferente tipo se realiza una conversión implícita que debe existir para que el compilador cree el código necesario automáticamente. Recuerde que siempre podrá realizar un cast explícito.

EJERCICIOS:

Tarea 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int n, b;
Console.WriteLine("escriba los numeros");
n=Convert.ToInt16 (Console.ReadLine());
b=Convert.ToInt16 (Console.ReadLine());

if (n <= b) { Console.WriteLine("el menor es "+n); } else if(b<=n) { Console.WriteLine("el menor es " + b); } else if(n>=b)
{
Console.WriteLine("el mayor es " + n);
}
else if (b >= n)
{
Console.WriteLine("el mayor es " + b);
}
Console.ReadLine();

}
}
}

___________________________________________________________________
Tarea2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int n ;
Console.WriteLine("cuanto desea retirar del cajero");
Console.WriteLine("digite una de estas cantidades");
Console.WriteLine("100-90-80-70-60-50-40-30-20-10");

n = Convert.ToInt16(Console.ReadLine());
if (n == 100)
{
Console.WriteLine("1 billete de 100 soles o ");
Console.WriteLine("5 billetes de 20 soles o");
Console.WriteLine("2 monedas de 5 soles ");
}
else if (n == 90)
{
Console.WriteLine("4 billetes de 20 y 2 monedas de 5 soles");
}
else if (n == 80)
{
Console.WriteLine("4 billetes de 20 soles");
}
else if (n == 70)
{
Console.WriteLine("3 billetes de 20 soles y 2 monedas de 5 soles");
}
else if (n == 60)
{
Console.WriteLine("3 billetes de 20 soles");
}
else if (n == 50)
{
Console.WriteLine("2 billetes de 20 soles y 2 monedas de 5 soles");
}
else if (n == 40)
{
Console.WriteLine("4 billetes de 20 soles");
}
else if (n == 30)
{
Console.WriteLine("1 billetes de 20 soles y 2 monedas de 5 soles");
}
else if (n == 20)
{
Console.WriteLine("1 billetes de 20 soles");
}
else if (n == 10)
{
Console.WriteLine(" 2 monedas de 5 soles");
}
else
{
Console.WriteLine(" escriba un monto entero");
}
Console.ReadLine();



}
}
}

________________________________________________________________

Tarea 3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int n ;
Console.WriteLine("cuanto desea retirar del cajero");
Console.WriteLine("digite una de estas cantidades");
Console.WriteLine("100-90-80-70-60-50-40-30-20-10");

n = Convert.ToInt16(Console.ReadLine());
if (n == 100)
{
Console.WriteLine("1 billete de 100 soles o ");
Console.WriteLine("5 billetes de 20 soles o");
Console.WriteLine("2 monedas de 5 soles ");
}
else if (n == 90)
{
Console.WriteLine("4 billetes de 20 y 2 monedas de 5 soles");
}
else if (n == 80)
{
Console.WriteLine("4 billetes de 20 soles");
}
else if (n == 70)
{
Console.WriteLine("3 billetes de 20 soles y 2 monedas de 5 soles");
}
else if (n == 60)
{
Console.WriteLine("3 billetes de 20 soles");
}
else if (n == 50)
{
Console.WriteLine("2 billetes de 20 soles y 2 monedas de 5 soles");
}
else if (n == 40)
{
Console.WriteLine("4 billetes de 20 soles");
}
else if (n == 30)
{
Console.WriteLine("1 billetes de 20 soles y 2 monedas de 5 soles");
}
else if (n == 20)
{
Console.WriteLine("1 billetes de 20 soles");
}
else if (n == 10)
{
Console.WriteLine(" 2 monedas de 5 soles");
}
else
{
Console.WriteLine(" escriba un monto entero");
}
Console.ReadLine();



}
}
}

--------------------------------------------------------------------------------------------------------------------

escribir un programa que pida n numeros y nos diga cual es el mayor de todos ellos, cual es el menor de todos ellos, y en que posicion fueron leidos cada uno.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int n, b;
Console.WriteLine("escriba los numeros");
n=Convert.ToInt16 (Console.ReadLine());
b=Convert.ToInt16 (Console.ReadLine());

if (n <= b) { Console.WriteLine("el menor es "+n); } else if(b<=n) { Console.WriteLine("el menor es " + b); } else if(n>=b)
{
Console.WriteLine("el mayor es " + n);
}
else if (b >= n)
{
Console.WriteLine("el mayor es " + b);
}
Console.ReadLine();

}
}
}


escribir un programa que preesente a pantalla de la tabla de comunicacion desde el uo hasta el diez.



hacer un progrma que pida un monto de dinero, luego se tratara de dar la menor cantidad de billetes posibles, se dispone de billetes de 100soles,20soles y monedas de 5 soles.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int n ;
Console.WriteLine("cuanto desea retirar del cajero");
Console.WriteLine("digite una de estas cantidades");
Console.WriteLine("100-90-80-70-60-50-40-30-20-10");

n = Convert.ToInt16(Console.ReadLine());
if (n == 100)
{
Console.WriteLine("1 billete de 100 soles o ");
Console.WriteLine("5 billetes de 20 soles o");
Console.WriteLine("2 monedas de 5 soles ");
}
else if (n == 90)
{
Console.WriteLine("4 billetes de 20 y 2 monedas de 5 soles");
}
else if (n == 80)
{
Console.WriteLine("4 billetes de 20 soles");
}
else if (n == 70)
{
Console.WriteLine("3 billetes de 20 soles y 2 monedas de 5 soles");
}
else if (n == 60)
{
Console.WriteLine("3 billetes de 20 soles");
}
else if (n == 50)
{
Console.WriteLine("2 billetes de 20 soles y 2 monedas de 5 soles");
}
else if (n == 40)
{
Console.WriteLine("4 billetes de 20 soles");
}
else if (n == 30)
{
Console.WriteLine("1 billetes de 20 soles y 2 monedas de 5 soles");
}
else if (n == 20)
{
Console.WriteLine("1 billetes de 20 soles");
}
else if (n == 10)
{
Console.WriteLine(" 2 monedas de 5 soles");
}
else
{
Console.WriteLine(" escriba un monto entero");
}
Console.ReadLine();



}
}
}

'¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡


SENTENCIA WHILE EN PROGRAMACION DE C#

DEFINICION:
La Sentencia while se utiliza cuando no se conoce previamente cuantas veces ha de repetirse un bloque de código, por lo que puede ejecutarse 0 o más veces. Este bloque se repetira mientras la condición evalue una expresión booleana verdadera, no será posible evaluar otro tipo de expresión.

while(condicional){}

Ejemplo:

using System;
using System.IO;

class SentenciaWhile{
public static void Main(){
if(!File.Exists("test.html")){
Console.WriteLine("El archivo test.html no existe");
return;
}
StreamReader SR = File.OpenText("test.html");
String strLinea = null;
while(null != (strLinea = SR.ReadLine())){
Console.WriteLine(strLinea);
}
SR.Close();
}
}

Es posible utilizar la sentencia break para salir del ciclo o continue para saltar una iteración.

EJERCICIOS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// hacer un programa que permita ingresar notas siempre y cuando sean mayores a 10
int nota;
do
{
Console.Write("ingrese notas :");
nota = Convert.ToInt16(Console.ReadLine());

}
while (nota > 10);

Console.WriteLine("no puede ingresar notas...");
Console.ReadLine();
}
}
}

______________________________________________________________________________________
hacer un programa que pida n notas mientras seam mayores al promedio
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// hacer un programa que pida n notas mientras seam mayores al promedio
int nota, suma=0,promedio=0, cont = 0;

do
{
cont++;
Console.Write("ingrese notas :");
nota = Convert.ToInt16(Console.ReadLine());
suma = suma + nota;
promedio = suma / cont;
Console.WriteLine("el promedio es :"+promedio);
Console.WriteLine("========================");
}while (nota >= promedio);


Console.WriteLine("no puede ingresar notas...");
Console.ReadLine();
}
}
}
______________________________________________________________

hacer un programa que acepte cualquier cantidad de numeros mientras estos sean pares
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// hacer un programa que pida n notas mientras seam mayores al promedio
int n;

do
{

Console.Write("ingrese numero :");
n = Convert.ToInt16(Console.ReadLine());


}while (n%2==0);


Console.WriteLine("el numero ingresado es impar...");
Console.ReadLine();
}
}
}
_____________________________________________________________________________________
// hacer un programa que concatene una oracion interrogatiba
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// hacer un programa que concatene una oracion interrogatiba
string n, s="";


do
{

Console.Write("ingrese palabra:");
n = Console.ReadLine();
s = s + n;
}while (n!="?");


Console.WriteLine( s);
Console.ReadLine();
}
}
}

ALGORITMOS

ARREGLOS EN PROGRAMACION DE C#

Un arreglo contiene variables a las cuales se accede a través de índices, todas las variables contenidas en el arreglo son referidos como elementos los cuales deben ser del mismo tipo, por lo que el tipo del arreglo.

Los arreglos en C# son referencias a objetos. Un arreglo value type no contiene instancias boxed.

El valor inicial de un arreglo es null, un arreglo de objetos es creado utilizando new.

Cuando un arreglo es creado inicialmente contiene los valores por default para los tipos que este contendrá.

Sintaxis:

tipo[] identificador;

Note que para definir un arreglo se utilizan los corchetes [] después del tipo del arreglo.

Ejemplo:

string[] aPersonas;

Es posible inicializar un arreglo al momento de crearlo:

string[] asPersonas = new string[] {"Tim Berners-Lee","Brendan Eich","Dennis Ritchie","James Gosling"};

Durante la inicialización es posible omitir new tipo[x] y el compilador podría determinar el tamaño de almacenamiento para el arreglo del número de items en la lista de inicialización:

string[] asPersonas = {"Tim Berners-Lee","Brendan Eich","Dennis Ritchie","James Gosling"};

Cada elemento de un arreglo de ints es un int con el valor 0:

int[] aiNumeros = new int[5];

Cada elemento de un arreglo de strings es un string con el valor null:

string[] asNombres = new string[5];


EJERCICIOS:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] palabras = new string[10];
for (int i = 0; i < 10; i++) // sirve para escribir n numeros
{
Console.Write("ingre el nombre {0} : ", i);
palabras [i]= Console.ReadLine();// imprime las palabras para poder escribir
}
string b; // agrego una variable b para para buscar la palabra
Console.Write("ingrese la palabra buscar : ");
int encontro = -1; // esto srive como bandera
int repetir = 0;
b = Console.ReadLine();
for (int j = 0; j < 10; j++) // sirve para buscar la palabra
{
if (palabras[j] == b)
{
encontro = j;// se encuentra la palabra y sies igual a j que esta entre
//los diez, reemplazsara hasta encontrar la palabra j
repetir++;// se repite para poner las veces que se repiten
}
}
if (encontro == -1) // coomo encontro es una absendara, dice que busqye a partir de -1 la varialbe y la escriba
{
Console.WriteLine("no se encontro lo buscado ");
}
else
{
Console.WriteLine("se encontro la palabra {0} en la poisicion {1} y se repite {2}",b,encontro,repetir);
}
Console.ReadLine();

}
}
}

---------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[,] matriz = { { 6, 7 }, { 8, 9 } };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
Console.Write(matriz[i, j]);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

---------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[,] matriz = { { 5,6, 7 }, { 8, 9,10 } };
for (int i = 0; i < 2; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 3; j++)// j: siempre van hacer las columnas
{
Console.Write(matriz[i , j] + " ");// el mas es un salto de linea
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

-------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[,] matriz = { { 5,15, 20,25}, {3,6,9,12 }, {8,16,24,32 } };
for (int i = 0; i < 3; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 4; j++)// j: siempre van hacer las columnas
{
Console.Write(matriz[i , j] + " ");// el mas es un saltyo de linea
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
----------------------------------------------
se pide los valores con los teclados

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[,] matriz = new int [3,4];
// para pedir por pantalla:
for (int i = 0; i < 3; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 4; j++)// j: siempre van hacer las columnas
{
Console.Write("matriz [{0},{1}]= ", i, j);// con el white imprime al costado y con el whiteline se imprime abajo
matriz[i, j] = Convert.ToInt16(Console.ReadLine());
}
}

// para imprimir
for (int i = 0; i < 3; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 4; j++)// j: siempre van hacer las columnas
{
Console.Write(matriz[i , j] + "\t");// el mas es un salto de linea y el \t son para q salga un poco mas ordenada la matriz
}
Console.WriteLine();// para que de un salo de linea
}
Console.ReadLine();
}
}
}

-----------------------------------------------------------------
para que imprima que salga la matriz ya impresa sin estar poniendo los valores con teclado:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random objeto = new Random();
int[,] matriz = new int [3,4];
// para pedir por pantalla:
for (int i = 0; i < 3; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 4; j++)// j: siempre van hacer las columnas
{
matriz[i, j] = objeto.Next(20);
}
}

// para imprimir
for (int i = 0; i < 3; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 4; j++)// j: siempre van hacer las columnas
{
Console.Write(matriz[i , j] + "\t");// el mas es un salto de linea y el \t son para q salga un poco mas ordenada la matriz
}
Console.WriteLine();// para que de un salo de linea
}


Console.ReadLine();
}
}
}


----------------------------------------------------------------------------------------------------
imprimir solo los pares:
\t=para espacio
\t=para saltar de un lugar a otro.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random objeto = new Random();
int[,] matriz = new int [3,4];
// para pedir por pantalla:
for (int i = 0; i < 3; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 4; j++)// j: siempre van hacer las columnas
{
matriz[i, j] = objeto.Next(20);
}
}

// para imprimir
for (int i = 0; i < 3; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 4; j++)// j: siempre van hacer las columnas
{
Console.Write(matriz[i , j] + "\t");// el mas es un salto de linea y el \t son para q salga un poco mas ordenada la matriz
}
Console.WriteLine();// para que de un salo de linea
}
int[] pares = new int[12];
int c = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
if (matriz[i, j] % 2 == 0)
{
pares[c] = matriz[i, j];
c++;
}
}
}
Console.WriteLine("======================================================");
for (int k = 0; k < c; k++)
Console.Write(pares[k] + "\t");
Console.WriteLine("\n=====================================================");

Console.ReadLine();
}
}
}

--------------------------------------------------------------------------------

hacer un arreglo que imprima los impares:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random objeto = new Random();
int[,] matriz = new int [3,4];
// para pedir por pantalla:
for (int i = 0; i < 3; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 4; j++)// j: siempre van hacer las columnas
{
matriz[i, j] = objeto.Next(20);
}
}

// para imprimir
for (int i = 0; i < 3; i++)// i: siempre van hacer las filas.
{
for (int j = 0; j < 4; j++)// j: siempre van hacer las columnas
{
Console.Write(matriz[i , j] + "\t");// el mas es un salto de linea y el \t son para q salga un poco mas ordenada la matriz
}
Console.WriteLine();// para que de un salo de linea
}
int[] pares = new int[12];
int c = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
if (matriz[i, j] % 2 == 0)
{
pares[c] = matriz[i, j];
c++;
}
}
}
Console.WriteLine("======================================================");
for (int k = 0; k < c; k++)
Console.Write(pares[k] + "\t");
Console.WriteLine("\n=====================================================");

int[] impares = new int[12];
int a = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
if (matriz[i, j] % 2 != 0)
{
impares[a] = matriz[i, j];
a++;
}
}
}


Console.WriteLine("\n=====================================================");
Console.WriteLine("\t\nimpares");
for (int k = 0; k < a; k++)
Console.Write(impares[k] + "\t");
Console.WriteLine("\n=====================================================");



Console.ReadLine();
}
}
}

---------------------------------------------------------------------------------------------------------------------
leer los numeros de:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[10];

// int[] a = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
int total = 0;
for (int i = 0; i < a.Length; i++)
{
a[i] = Convert.ToInt16(Console.ReadLine());
total += a[i];
Console.WriteLine("suma parcial "+total);
}
Console.WriteLine("Suma de todos los elementos del array: " + total);
Console.ReadLine();
}
}

--------------------------el mismo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[10];

// int[] a = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
int total = 0;
for (int i = 0; i < 10; i++)
{
Console.WriteLine("ingrese el numero");
a[i] = Convert.ToInt16(Console.ReadLine());
total += a[i];
Console.WriteLine("suma parcial "+total);
}
Console.WriteLine("Suma de todos los elementos del array: " + total);
Console.ReadLine();
}
}
}

--------------------------------------------------------------------
ingresar notas:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[] notas = new int[10];
int[] frecuencia = new int[21];
for (int j = 0; j < notas.Length; j++)
{
Console.Write("Ingrese Nota==>");
notas[j] = Convert.ToInt16(Console.ReadLine());
frecuencia[notas[j]]++;
}
for (int i=0; i<21;i++)
Console.WriteLine( "la frecuencia de las nota {0} = {1}",i,frecuencia [i]);
Console.ReadLine();
}
}
}

----------------------------------------------------------------------------------------------------
arreglos ordenados:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class arregla50elementos
{
static void Main(string[] args)
{
Random objeto = new Random();
int []arreglo= new int[50];
for (int i = 0; i < 10; i++)
{
arreglo[i]=objeto.Next(100);
Console.WriteLine( arreglo[i]);
}
int temp;
for (int i = 0; i < 10; i++)
{

for (int j = 0; j < 10; j++)
{
// comprando parejas de numeros
if (arreglo[j] > arreglo[j + 1])
{
// asiganando valores ordenados.

temp = arreglo[j];
arreglo[j] = arreglo[j + 1];
arreglo[j + 1] = temp;

}
}
}

Console.WriteLine("arreglo ordenado");
for (int i = 0; i < 10; i++)
{

Console.WriteLine(arreglo[i]);
}
Console.ReadLine();
}
}
}

--------------------------------------------------------------------------
MULTIPLICACION:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{

int[,] a = new int[4, 4];
int[,] b = new int[4, 4];
int[,] c = new int[4, 4];

Random objeto = new Random();
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{

a[i, j] = objeto.Next(20);
}
}

for (int i = 0; i < 4; i++)//i es filas
{
for (int j = 0; j < 4; j++)// j es columna //ve columna
{
Console.Write(a[i, j] + "\t");//para q imprima la matriz

}
Console.WriteLine();//para que de un salto de linea
}
Console.WriteLine("===========================================");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{

b[i, j] = objeto.Next(20);
}
}
for (int i = 0; i < 4; i++)//i es filas
{
for (int j = 0; j < 4; j++)// j es columna //ve columna
{
Console.Write(b[i, j] + "\t");//para q imprima la matriz

}
Console.WriteLine();//para que de un salto de linea
}
Console.WriteLine("========================================================");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
c[i, j] = 0;
for (int k = 0; k < 4; k++)
{
c[i, j] = c[i, j] + (a[i, k] * b[k, j]);

}
}
}
for (int i = 0; i < 4; i++)//i es filas
{
for (int j = 0; j < 4; j++)// j es columna //ve columna
{
Console.Write(c[i, j] + "\t");
}
Console.WriteLine();
Console.ReadLine();

}
}
}
}