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

If your algorithm is implemented correctly, all you should have to do is reverse your comparison to change the sort order.

sorry wrong code.

this is my actual code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main () 
{
    double values[50];
	int quantity, count, a, b;
	
	count = 1;
	
	
	cout << "how many values do you want to sort (max 50): ";
	cin >> quantity;
	
	if (quantity >50)
    do
    {
	cout << quantity << " is too many values, please re-enter a value of 50 or less: ";
	cin >> quantity;
    }
    while (quantity >50);
		
		
	do
	{
		cout << "Enter value number " << count << ": ";
		cin >> values[count];
		count++;
	}
	while (count != quantity+1);
	
	
	
	for (a=1; a < count; a++) {
		for (b=count-1; b>=a; b--) {
			if (values[b-1] > values[b]) { 
				a = values[b-1];
				values[b-1] = values[b];
				values[b] = a;

			}
		}
	}
	
	cout << "here are your values: " << endl;
	for (a=1; a < count; a++)
	cout << values[a] << "\n";
	
	return 0;
}

Your program has a problem at the accepting user input. Your count start from 1 but an array index starts from 0. Then you are attempt to assign the last index which does not exist! Initiate the count with 0 and the while condition to (count<quantity).

To do the descending, look at your line 38 and follow what Fbody said...

Here is what i have now.

It wont order them correctly in ascending order.

i think there is something wrong with my bubble sort code.

#include <iostream>
#include <cstdlib>

using namespace std;

int main () 
{
    double values[50];
	int quantity, count, a, b;
	
	count = 0;
	
	
	cout << "how many values do you want to sort (max 50): ";
	cin >> quantity;
	
	if (quantity >50)
    do
    {
	cout << quantity << " is too many values, please re-enter a value of 50 or less: ";
	cin >> quantity;
    }
    while (quantity >50);
		
		
	do
	{
		cout << "Enter value number " << count+1 << ": ";
		cin >> values[count];
		count++;
	}
	while (count<quantity);
	
	
	
	for (a=1; a < count; a++) 
	{
		for (b=count-1; b>=a; b--) 
		{
			if (values[b-1] > values[b]) 
			{ 
				a = values[b-1];
				values[b-1] = values[b];
				values[b] = a;

			}
		}
	}
	
	cout << "here are your values: " << endl;
	for (a=1; a < count; a++)
	cout << values[a] << "\n";
	
	return 0;
}
do
    {
	cout << quantity << " is too many values, please re-enter a value of 50 or less: ";
	cin >> quantity;
    }
    while (quantity >50);

This should not be a do-while loop. It should be a regular while loop.

for (a=1; a < count; a++) 
	{
		for (b=count-1; b>=a; b--) 
		{
			if (values[b-1] > values[b]) 
			{ 
				a = values[b-1];
				values[b-1] = values[b];
				values[b] = a;

			}
		}
	}

It looks like you have the basic idea, but you need to pay closer attention to your variable names. When you perform the swap, you should use a temporary variable, not "a". By using "a" for your temporary, you are seriously messing up your loop.

when i do these adjustments it doesnt count the first entry.

entering the values is fine, its just sorting them, it only sorts them a little and leaves them at that

also i use the do .. while loop so it will repeat every time you enter a value over 50. in a standard while loop, it only repeats once.

when i do these adjustments it doesnt count the first entry.

entering the values is fine, its just sorting them, it only sorts them a little and leaves them at that

Like Taywin said, that's because you are starting at 1 instead of 0. In C++ array indexes start at 0 NOT 1. For your loops to work properly, you have to take this fact into account.

also i use the do .. while loop so it will repeat every time you enter a value over 50. in a standard while loop, it only repeats once.

Then you didn't implement the loop correctly. Show me your implementation of a standard while.

With a do loop, execution of the loop is guaranteed to occur at least once. Which means that even if your user enters a valid value, your program will say it's an invalid value and require the user to re-enter it.

Here is an example of a proper implementation of an ascending Bubble sort from a project of mine. DO NOT attempt to copy it, it is not compatible with your program.

//Bubble sort
template <typename T> void DataSet<T>::BubbleSort() {
  for (int i = 0; i < m_DataSet.size(); ++i) {
    bool swap = false;
    for (int j = (m_DataSet.size()-1); j > i; --j) {
      if (m_DataSet[j] < m_DataSet[j-1]) {
        std::swap(m_DataSet[j], m_DataSet[j-1]);
        swap = true;
      }
    } //end for (index j)
    if (!swap)
      break;
  } //end for (index i)
} //end DataSet<T>::BubbleSort()

i have changed it to a standard while loop and have started my count from zero now, but the first value entered is ignored by the program when sorting.

its also still not sorting them correctly.

Here is what i have now.

#include <iostream>
#include <cstdlib>

using namespace std;

int main () 
{
    double values[50];
	int quantity, count, a, b;
	
	count = 0;
	
	
	cout << "how many values do you want to sort (max 50): ";
	cin >> quantity;
	
	while (quantity >50)
	{
	cout << quantity << " is too many values, please re-enter a value of 50 or less: ";
	cin >> quantity;
    }
    
	
		
	do
	{
		cout << "Enter value number " << count << ": ";
		cin >> values[count];
		count++;
	}
	while (count<quantity);
	
	
	
	for (a=1; a < count; a++) {
		for (b=count-1; b>=a; b--) {
			if (values[b-1] < values[b]) { 
				a = values[b-1];
				values[b-1] = values[b];
				values[b] = a;

			}
		}
	}
	
	cout << "here are your values: " << endl;
	for (b=1; b < count; b++)
	cout << values[b] << "\n";

	return 0;
}

Without even looking back I remember someone pointing out that you are destroying your loop counter a, so naturally the outer loop is broken...

Use a temporary variable to swap values like

for (a=1; a < count; a++) 
        for (b=count-1; b>=a; b--) 
            if (values[b-1] < values[b]) { 
                                       int temp=values[b-1];
                                       values[b-1]=values[b];
                                       values[b]=temp;}

Then it will be a correct code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.