jueves, 26 de noviembre de 2009

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

}
}
}
}

No hay comentarios:

Publicar un comentario