viernes, 27 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 < 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();

}
}
}
}

No hay comentarios:

Publicar un comentario