Hacer un programa o algoritmo. Se tiene un arreglo "A" donde se encuentra 10 números ordenados en forma ascendente, y se pide que el algoritmo me permita el ingreso de un número y lo coloque en la posición donde debería de estar con respecto al orden del arreglo.
Codigo Fuente en C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Main
{
class Program
{
static void Main(string[] args)
{
int []lista=new int[] {1, 25, 28,31, 56, 98, 132, 195, 282, 502};
int[] buf=new int[12];
int w = 0, C, T, tem, n;
String cad = "";
ArrayList listaen = new ArrayList();
for (int q = 0;q<lista.Length;q++)
{
listaen.Add(lista[q]);
}
foreach (Object obj in listaen)
Console.Write(" {0}", obj);
Console.WriteLine();
Console.WriteLine("Ingrese la cantidad de numeros a agregar");
C=Convert.ToInt16(Console.ReadLine());
T = C + 10;
for (int i = 10; i < T; i++)
{
Console.WriteLine("ingresa num");
int dat = Convert.ToInt16(Console.ReadLine());
listaen.Add(dat);
}
for (int r = 0; r < T; r++)
{
buf[r] = Convert.ToInt32(listaen[r]);
}
//ordenamos el arreglo algoritmo burbuja
while (w != (T - 1))
{
w = 0;
for (int k = 0; k < (T - 1); k++)
{
if (buf[k] <= buf[k + 1])
{
w++;
}
else
{
tem = buf[k];
buf[k] = buf[k + 1];
buf[k + 1] = tem;
}
}
}
Console.WriteLine("Mostrar numeros ordenados");
for (int rr = 0; rr < T; rr++)
{
cad = cad +" "+ buf[rr];
}
Console.WriteLine(cad);
Console.ReadLine();
}
}
}
2 comentarios:
Gracias por el aporte!
Debes cambiar el titulo del post ...en realidad tu post se debe llamar ingresar elementos en un array....si dices N es xq el usuario debe ingresar el numero de elementos, crear el array, y luego q por consola ingrese elemento pro elemento
Publicar un comentario