Thread: bubble sort with strings

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    bubble sort with strings

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void sort(int array[], string months[], int size);
    
    int main()
    {
    	string months[] = {"January",
                           "February",
                           "March",
                           "April",
                           "May",
                           "June",
                           "July",
                           "August",
                           "September",
                           "October",
                           "November",
                           "December"};
    	
    	int rainfall[12];
    
    	cout << "Enter the monthly rainfall for each month" << endl;
    
    	for (int i = 0; i < 12; i++)
    	{
    		cout << months  [i] << ": ";
    		cin  >> rainfall[i];
    
    		while (cin.fail() || rainfall[i] < 0)
    		{
    			cin.clear();
    			cin.ignore(INT_MAX, '\n');
    
    			cout << "Invalid entry" << endl;
    			cout << "Re-enter"      << endl;
    
    			cout << months  [i] << ": ";
    			cin  >> rainfall[i];
    		}
    	}
    
    	sort(rainfall,  months, 12);
    
    	cout << "Here are the months with the highest" 
    		 << " to lowest rainfall:" << endl;
    
    	for (i = 0; i < 12; i++)
    		cout << months[i] << endl;
    
    	return 0;
    }
    
    void sort(int array[], string months[], int size)
    {
    	int hold;
    	string hold2;
    	int finish;
    
    	do
    	{
    		finish = 0;
    
    		for (int i = 0; i < size - 1; i++)
    		{
    			if (array[i] > array[i + 1])
    			{
    				hold2         = months[i];
    				months[i]     = months[i + 1];
    				months[i + 1] = hold2;
    
    				hold          = array[i];
    				array[i]      = array[i + 1];
    				array[i + 1]  = hold;
    			}
    		}
    
    			finish = 1;
    
    	}while (finish != 0);
    }
    After all of the data has been entered, the program doesn't do anything. I think the months array is causing the problem.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    do
    {
       ....
       ....
    
       finish = 1;
    
    }while (finish != 0);
    How many times will that loop execute?

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Uh...infinite times...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Psuedo random numbers in a bubble sort.
    By Sir Andus in forum C++ Programming
    Replies: 10
    Last Post: 04-09-2007, 08:25 PM
  2. bubble sort help.
    By zeromx in forum C Programming
    Replies: 9
    Last Post: 10-30-2006, 07:37 AM
  3. bubble sort not working with struct...
    By Jenica in forum C++ Programming
    Replies: 1
    Last Post: 10-12-2004, 12:54 PM
  4. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04:48 AM
  5. Radix Sort, Strings, and Linked Lists
    By dark paladin in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 03:24 PM