Thread: sorting arrays

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45

    sorting arrays

    hello everyone,

    I was wondering if there is way to sort two different arrays of different data types without struct's.
    For example lets say you have an array of doubles that represent rainfall for each month of the year. [0] representing January's rainfall amount, etc... Then I have an array of char's with the names of the months. [0] representing "January". Now I sort the rainfall in descending order, how do I get the char array to stay in sync with the rainfall array. I was planning on using a selection method to sort the rainfall amounts, but I can't pass it a double array and a char array.
    I know I could create another array of int's and use a selection method to sort rainfall and int's 1-12 to represent the months. Then I could pass this to a switch statement or loop to display the months rainfall. This way seems to show my inexperience. I know there has to be a way to sort them without any special libs.

    thanks for your time,
    Brad
    Last edited by bradleyd; 06-17-2008 at 12:54 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would create say, a MonthlyRainfall class (or struct) that contains the month name and the rainfall for that month. Then I define operator< (or a function object predicate) that sorts MonthlyRainfall objects by the rainfall. After that, I apply std::sort and my work is done.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    thanks for the reply. Unfortunately I can't use struct's. I have to do this with just arrays, or vector's.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, don't sort the data themselves, sort the indexes. So your double array won't change; your char * array won't change, but you'll have index[0]=6, index[1]=4, and so on.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Unfortunately I can't use struct's. I have to do this with just arrays, or vector's.
    Why? It is generally not so good to maintain parallel containers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    this is a class assignment and we have not gotten to struct's yet.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    Here is the osrt method I am using. I created an array of int's to try it out for now.
    I am passing it month[] which is an array of int's(1-12) and the rainfall amounts the user has entered, plus the size of the arrays.

    Code:
    void dualSort(int month[], double precip[], int size) {
    	
    	int start, maxIndex, tempMonth;
    	double maxAmount;
    	
    	for (start = 0; start < (size - 1); start++) {
    		maxIndex = start;
    		maxAmount = precip[start];
    		tempMonth = month[start];
    		for (int index = start + 1; index < size; index++) {
    			if (precip[index] > maxAmount) {
    				maxAmount = precip[index];
    				tempMonth = month[index];
    				maxIndex = index;
    			}
    		}
    		precip[maxIndex] = precip[start];
    		month[maxIndex] = month[start];
    		precip[start] = maxAmount;
    		month[start] = tempMonth;
    	}
    }

  8. #8
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    So, don't sort the data themselves, sort the indexes. So your double array won't change; your char * array won't change, but you'll have index[0]=6, index[1]=4, and so on.
    Will the data still come out in descending order? I gues I am unsure what you mean.
    Thanks for the reply.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Will the data still come out in descending order? I gues I am unsure what you mean.
    Of course, if you access the indices in sorted order. Alternatively, you can sort both the vectors at once, i.e., whenever you copy or swap a double that represents a rainfall amount, you copy or swap the corresponding string that represents the month name.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    I will give it a try and post back. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Sorting Arrays
    By DaniiChris in forum C Programming
    Replies: 11
    Last Post: 08-03-2008, 08:23 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM