Hi, i have to write a program that gets users to enter some numbers and it has to sort them in descending order. I've nearly finished but i just cant get it to sort in descending order, only ascending (using bubble sort).
Here is the program:
#include <cstdlib>
#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(int argc, char *argv[])
{
cout << "\t\t\tBUBBLE SORT IN DECENDING ORDER";
cout << "\n\t\t Created By: Mr. Jake R. Pomperada, MAED-IT";
cout << "\n\n";
cout << "How Many Items :=> ";
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] << " ";
}
cout << "\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
could you help me to sort it in descending order.
thank you very much