Thread: sorting

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

    sorting

    What am I doing wrong with this sorting algorithm? The months aren't being displayed properly.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void sort(int arry[], int size);
    
    int main()
    {
    	string months[] = {"January",
    		               "February",
    					   "March",
    					   "April",
    					   "May",
    					   "June",
    					   "July",
    					   "August",
    					   "September",
    					   "October",
    					   "November",
    					   "December"};
    
    	int    rainfall[12];
    
    	int highest = INT_MIN;
    	int lowest  = INT_MAX;
    
    	int high;
    	int low;
    
    	cout << "Enter the monthly rainfall for each month" << endl;
    
    	for (int i = 0; i < 12; i++)
    	{
    		cout << months  [i] << ": ";
    		cin  >> rainfall[i];
    
    		if (cin.fail() || rainfall[i] < 0)
    		{
    			cin.clear();
    			cin.ignore(INT_MAX, '\n');
    
    			cout << "Error...re-enter..." << endl;
    			cout << months  [i]           << ": ";
    		    cin  >> rainfall[i];
    		}	
    		
    		if (rainfall[i] > highest)
    		{
    			highest = rainfall[i];
    			high   = i;
    		}
    
    		if (rainfall[i] < lowest)
    		{
    			lowest = rainfall[i];
    			low    = i;
    		}
    		
    		sort(rainfall, 12);
    	}
    
        
    
    	cout << "\nThe month with the highest rainfall is " 
    		 << months[high]             << endl;
    
    	cout << "\nThe month with the lowest rainfall is "  
    		 << months[low]              << endl;
    
    	cout << "\nHere are the months sorted from highest rainfall" 
    		 << " to smallest rainfall:" << endl;
    
    	for (i = 0; i < 12; i++)
    		cout << months[high--]       << endl;
    
    	return 0;
    }
    
    void sort(int array[], int size)
    {
    	int swap;
    	int temp;
    
    	do
    	{
    		swap = 0;
    
    		for (int i = 0; i < size - 1; i++)
    		{
    			if (array[i] > array[i +1])
    			{
    				temp         = array[i];
    				array[i]     = array[i + 1];
    				array[i + 1] = temp;
    				swap         = 1;
    			}
    		}
    	} while (swap != 0);
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    cout << months[high--] << endl;

    Should be:

    cout << months[i] << endl;
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by XSquared
    cout << months[high--] << endl;

    Should be:

    cout << months[i] << endl;
    That doesn't work either. Your way just sorts the months from the first month to the last month. I'm trying to sort them by which month has the highest rainfall to the month with the smallest rainfall.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    void sort(int array[], string array2[], int size)
    {
    	int swap;
    	string tempS;
    	int temp;
    
    	do
    	{
    		swap = 0;
    
    		for (int i = 0; i < size - 1; i++)
    		{
    			if (array[i] < array[i +1])
    			{
    				tempS         = array2[i];
    				array2[i]     = array2[i + 1];
    				array2[i + 1] = tempS;
    				temp         = array[i];
    				array[i]     = array[i + 1];
    				array[i + 1] = temp;
    				swap         = 1;
    			}
    		}
    	} while (swap != 0);
    }
    And make sure to pass months as the 2nd parameter.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  3. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 04:21 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM
  5. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 08:05 PM