sorting arrays

This is a discussion on sorting arrays within the C++ Programming forums, part of the General Programming Boards category; hello everyone, I was wondering if there is way to sort two different arrays of different data types without struct's. ...

  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 01:54 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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
    22,166
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,166
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 12:27 PM
  2. Sorting Arrays
    By DaniiChris in forum C Programming
    Replies: 11
    Last Post: 08-03-2008, 09: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, 06:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21