viernes, 27 de noviembre de 2009

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();
}
}
}

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 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.

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();
}
}
}
}

METODO PARA RETORNAR EN C#

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

namespace ConsoleApplication1
{
class Program
{
public static int suma(int a, int b)
{
return a + b;// y restorna la suma a + b
}
static void Main(string[] args)
{
int a, b,c;// declara tres variables
a = 12;
b = 14;
c = suma(a, b);//suma es uh metodo q recibe dos valores
Console.WriteLine(suma(a, b));
a=16;
b=78;
c=suma(a,b);//suma es uh metodo q recibe dos valores
Console.WriteLine(suma(a, b));
c = suma(100, 200);//suma es uh metodo q recibe dos valores
Console.WriteLine(suma(a, b));
}
}
}

--------------------------------------------------
HAYAR EL FACTORIAL DE UN NUMERO


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

namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
int x, f=1;
Console.Write("INGRESE UN NUMERO");
x = Convert.ToInt16(Console.ReadLine());
for (int i = x; i > 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 < 10; i++)
{
Console.WriteLine("{0}[{1}]={2}", l, i, a[i]);
}
}

static void Main(string[] args)
{
Random objeto = new Random();
int[] a = new int[10];
int[] b = new int[10];
for (int i = 0; i < 10; i++)
{
a[i] = objeto.Next(12);
b[i] = fac(a[i]);
Console.WriteLine("EL FACTORIAL DE {0} ES ={1} ", a[i], b[i]);
}
imprimir(a,"a");
imprimir(b,"b");
}

}

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

namespace ConsoleApplication2
{
class Program
{
public static int suma(int a, int b)
{
return a + b;
}
public static int resta(int a, int b)
{
return a - b;
}
public static int multiplica(int a, int b)
{
return a * b;
}
public static int divide(int a, int b)
{
return a / b;
}

static void Main(string[] args)
{
Console.WriteLine(suma(8, 45));
Console.WriteLine(resta(8, 45));
Console.WriteLine(multiplica(8, 45));
Console.WriteLine(divide(8, 45));
}


}
}

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

RELLERNAR:se crea rellenar q permite rellenmar un arreglo de valores aleatorios


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

namespace ConsoleApplication2
{
class Program
{
public static int suma(int a, int b)
{
return a + b;
}
public static int resta(int a, int b)
{
return a - b;
}
public static int multiplica(int a, int b)
{
return a * b;
}
public static int divide(int a, int b)
{
return a / b;
}
public static void rellenar(int[] x, int r)// x toma el valor de a y r de b
{
Random objeto = new Random();// declara un objeto random
for (int i = 0; i < x.Length; i++)// length es tamaño
{
x[i] = objeto.Next(r);// cada uno de los elementos lo va a rellenar un ramdon
}
}
static void Main(string[] args)
{
int[] a = new int[10];// declarar 3 arreglos
int[] b = new int[10];
int[] c = new int[10];
rellenar(a, 20);// funcion rellear a con 20
rellenar(b, 40);
for(int i=0;i<10;i++)
{
c[i] = suma(a[i], b[i]);
Console.WriteLine("{0}+{1}={2}", a[i], b[i], c[i]);

}
}


}
}

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

RELLERNAR OCHO ARREGLOS:
a
b
c
d
e
f
g
h

x= mostrar la suma del arreglo a +b
y= la resta de c-d
z=la multiplicacion de e*f
w=la division de g/h
imrpimir donde se manda (a,b,c, simbolo "string")
imprimir (a,b,x,"+")
a+b=x

imprimir (c,d,y,"-")
c-d=y


console. whiteline("´(a) simbolo")

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

namespace ConsoleApplication2
{
class Program
{
public static int suma(int a, int b)
{
return a + b;
}
public static int resta(int a, int b)
{
return a - b;
}
public static int multiplica(int a, int b)
{
return a * b;
}
public static int divide(int a, int b)
{
return a / b;
}
public static void rellenar(int[] x, int r)// x toma el valor de a y r de b
{
Random objeto = new Random();// declara un objeto random
for (int i = 0; i < x.Length; i++)// length es tamaño
{
x[i] = objeto.Next(r);// cada uno de los elementos lo va a rellenar un ramdon
Console.WriteLine(x[i]);
}
}
public static void imprimir(int[] m, int[] n, int[] o, string s)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("{0} {1} {2} ={3}", m[i],s, n[i], o[i]);

}

}
static void Main(string[] args)
{
int[] a = new int[10];// declarar 3 arreglos
int[] b = new int[10];
int[] c = new int[10];
int[] d = new int[10];
int[] e = new int[10];
int[] f = new int[10];
int[] g = new int[10];
int[] h = new int[10];
int[] w = new int[10];
int[] y= new int[10];
rellenar(a, 20);// funcion rellear a con 20
Console.WriteLine("****************");
rellenar(b, 20);
rellenar(d, 20);
rellenar(e, 20);
rellenar(f, 20);
rellenar(g, 20);
rellenar(h, 20);
rellenar(w, 20);
rellenar(y, 20);

for(int i=0;i<10;i++)
{
c[i] = suma(a[i], b[i]);

}
imprimir(a, b, c, "+");

Console.WriteLine("****************");
for (int i = 0; i < 10; i++)
{
d[i] = resta(a[i], b[i]);


}

imprimir(a, b, d, "-");
Console.WriteLine("****************");
for (int i = 0; i < 10; i++)
{
c[i] =resta(a[i], b[i]);

}
imprimir(e, f, w, "*");
Console.WriteLine("******************************");
for (int i = 0; i < 10; i++)
{
w[i] = multiplica(e[i], f[i]);
}
imprimir(c, d, y, "/");
Console.WriteLine("********************************");
for (int i = 0; i < 10; i++)
{
y[i] = divide(c[i], d[i]);
}

}


}
}

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 < 4; i++)
{
for (int j = 0; j < 4; j++)
{

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

}
Console.WriteLine("\n\n\n");//para que de un salto de linea

}
Console.WriteLine("===================================================");
for (int i = 0; i < 4; i++)//i es filas
{
for (int j = 0; j < 4; j++)// j es columna //ve columna
{
if ( i==0 || i==3 || j==0 || j==3)
{
Console.Write(matriz[i, j] + "\t");
}
else
{
Console.Write("" + "\t");
}
}
Console.WriteLine("\n\n\n");//para que de un salto de linea
}


Console.ReadLine();

}
}
}
____________________________________________________________________________________
hacer una cruz de un cuadrado
*
*
*****
*
*
__________________________________________________________________________________
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[5, 5];
//ingresar
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{

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

}
Console.WriteLine("\n\n\n");//para que de un salto de linea

}
Console.WriteLine("===================================================");
for (int i = 0; i < 5; i++)//i es filas
{
for (int j = 0; j < 5; j++)// j es columna //ve columna
{
if ( i==2 || i==2 || j==2 || j==2)
{
Console.Write(matriz[i, j] + "\t");
}
else
{
Console.Write("" + "\t");
}
}
Console.WriteLine("\n\n\n");//para que de un salto de linea
}


Console.ReadLine();

}
}
}
______________________________________________________________________________________
hacer un rombo:

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

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

string[,] matriz = new string[5, 5];
//ingresar
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{

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

}
Console.WriteLine("\n\n\n");//para que de un salto de linea

}
Console.WriteLine("===================================================");
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (j == 2 || i == 2 ||((j==1 || j==3) && (i==1 || i==3)))
{
Console.Write(matriz[i, j] + "\t");
}
else
{
Console.Write("" + "\t");
}
}
Console.WriteLine("\n\n");
}


Console.ReadLine();

}
}
}

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

hacer una "x"


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

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

string[,] matriz = new string[5, 5];
//ingresar
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{

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

}
Console.WriteLine("\n\n\n");//para que de un salto de linea

}
Console.WriteLine("===================================================");
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (j == i ||(j+i==4))// es el unico cuya suma entre i+j es igual a 4
{
Console.Write(matriz[i, j] + "\t");
}
else
{
Console.Write("" + "\t");
}
}
Console.WriteLine("\n\n");
}


Console.ReadLine();

}
}
}

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 < encontro =" -1;" repetir =" 0;" b =" Console.ReadLine();" j =" 0;" encontro =" j;//" encontro ="=" matriz =" {" i =" 0;" j =" 0;" matriz =" {" i =" 0;" j =" 0;" matriz =" {" i =" 0;" j =" 0;" matriz =" new" i =" 0;" j =" 0;" i =" 0;" j =" 0;" objeto =" new" matriz =" new" i =" 0;" j =" 0;" i =" 0;" j =" 0;" t="para" t="para" objeto =" new" matriz =" new" i =" 0;" j =" 0;" i =" 0;" j =" 0;" pares =" new" c =" 0;" i =" 0;" j =" 0;" 2 ="=" k =" 0;" n="=" objeto =" new" matriz =" new" i =" 0;" j =" 0;" i =" 0;" j =" 0;" pares =" new" c =" 0;" i =" 0;" j =" 0;" 2 ="=" k =" 0;" n="=" impares =" new" a =" 0;" i =" 0;" j =" 0;" n="=" k =" 0;" n="=" a =" new" a =" {" total =" 0;" i =" 0;" a =" new" a =" {" total =" 0;" i =" 0;" notas =" new" frecuencia =" new" j =" 0;" nota="=">");
notas[j] = Convert.ToInt16(Console.ReadLine());
frecuencia[notas[j]]++;
}
for (int i=0; i<21;i++) objeto =" new" arreglo=" new" i =" 0;" i =" 0;" j =" 0;"> 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();

}
}
}
}