ayudenme a hacer este programa
necesito hacer un programa en donde yo introduzca 20 productos con un numero como una mesa y luego tener la opcion de ordenarlos de manera ascendente.
porfavor ayudenme o diganme que tengo que utlilizar.
Gracias
porfavor ayudenme o diganme que tengo que utlilizar.
Gracias
Te envio un programa que aplica el ordenamiento de Burbuja para 20 numeros generados aleatoriamente.
Puedes cambiar el programa sin dificultades para aceptar los numero desde el teclado.
Este programa muestra los diferentes estados del arreglo durante el ordenamiento, por supuesto que esto tambien puede modificarse.
Exito ! ! !
// program k4_5.CPP - page 70
// bubble sort.
// written in Borland CPP ver 3.1
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#define MAX 20
void Show(int *a)
{
for(int i = 0; i < MAX; i++)
cout << setw(4) << a[i];
cout << endl;
} // SHOW
void Init(int *a)
{
for(int i = 0; i < MAX; i++)
a[i] = random(100);
} // INIT
void Swap(int &a,int &b)
{
int c = a;
a = b;
b = c;
} // SWAPS INTEGER
void BubbleSort(int *a)
{
for(int top = MAX-1; top; top--)
{
for(int i = 0; i < top; i++)
if (a[i] > a[i+1])
Swap(a[i],a[i+1]);
Show(a);
}
} // BUBBLE SORT
void main()
{
clrscr();
randomize();
int a[MAX];
cout << "bubble sort : " << endl << endl;
Init(a);
Show(a);
cout << endl;
BubbleSort(a);
cout << endl << "end of program - good bye ! ! !n";
getch();
} // MAIN
Puedes cambiar el programa sin dificultades para aceptar los numero desde el teclado.
Este programa muestra los diferentes estados del arreglo durante el ordenamiento, por supuesto que esto tambien puede modificarse.
Exito ! ! !
// program k4_5.CPP - page 70
// bubble sort.
// written in Borland CPP ver 3.1
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#define MAX 20
void Show(int *a)
{
for(int i = 0; i < MAX; i++)
cout << setw(4) << a[i];
cout << endl;
} // SHOW
void Init(int *a)
{
for(int i = 0; i < MAX; i++)
a[i] = random(100);
} // INIT
void Swap(int &a,int &b)
{
int c = a;
a = b;
b = c;
} // SWAPS INTEGER
void BubbleSort(int *a)
{
for(int top = MAX-1; top; top--)
{
for(int i = 0; i < top; i++)
if (a[i] > a[i+1])
Swap(a[i],a[i+1]);
Show(a);
}
} // BUBBLE SORT
void main()
{
clrscr();
randomize();
int a[MAX];
cout << "bubble sort : " << endl << endl;
Init(a);
Show(a);
cout << endl;
BubbleSort(a);
cout << endl << "end of program - good bye ! ! !n";
getch();
} // MAIN
