Snippet
#include <iostream>
using namespace std;
int compare(int, int);
void sort(int[], const int);
void swap(int *, int *);
int compare(int x, int y)
{
return(x > y);
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void sort(int table[], const int n)
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n-1; j++)
{
if(compare(table[j], table[j+1]))
swap(&table[j], &table[j+1]);
}
}
}
int quantity;
int* tab;
int main()
{
cout << "Input quantity: ";
cin >> quantity;
tab = new int [quantity];
cout << "Input numbers: \n\n";
for (int i = 0; i < quantity; i++)
{
int x = i;
cout << "#" << ++x << ": ";
cin >> tab[i];
}
cout << "\nBefore sorting: ";
for (int i = 0; i < quantity; i++)
{
cout << tab[i] << " ";
}
cout << "\nAfter sorting: ";
sort(tab, quantity);
for(int i = 0; i < quantity; i++)
{
cout << tab[i] << " ";
}
return 0;
}
Copy & Paste
|