Thread: Sorting Multiple Arrays with complications of 2D arrays and strings.

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    1

    Sorting Multiple Arrays with complications of 2D arrays and strings.

    Hello,

    I am having trouble with an assignment at college. Now, I don't want anyone to write the program for me. I just want to understand the concepts.

    Problem Specification:
    I have to get a list of names and data to go with those names from an infile, sort them and then print them to the screen. The program works until I get to the actual sorting. It is returning weird values and not sorting anything. If someone could tell me what's wrong with the sort function and explain the concepts I don't have right?

    The code in question:
    Code:
    /*datalength is the number of entries from the datafile*/
    void SortData (int datalength, char full [50][40], char last [50][20], char first [50][20], double fall [], double winter [], double spring [], double summer [], double total [])
    {
    	int i;
    	int j;
    	char chartemp [40];
    	double doubletemp;
    	
    	char comma [2] = {","};
    	
    	/*Combines the last and first names into one array: full, and inserts a comma between them / Works*/
    	for (i = 0; i < datalength; i++)
    	{
    		strcpy (full [i], last [i]);
    		strcat (full [i], comma);
    		strcat (full [i], first [i]);
    	}
    	
    	/*The part that doesn't work, the lower variable value is giving weird or wrong answers*/
    	for (i = 0; i < datalength; i++)
    	{
    		lowest = i;
    		for (j = i + 1; j < datalength; j++)
    		{
    			if (strcmp (full [j], full [lowest]) < 0);
    				lowest = j;
    			if (i != lowest)
    			{
    				strcpy (chartemp, full [j]);
    				strcpy (full [j], full [i]);
    				strcpy (full [i], chartemp);
    			
    				doubletemp = fall [j];
    				fall [j] = fall [i];
    				fall [i] = doubletemp;
    				
    				doubletemp = winter [j];
    				winter [j] = winter [i];
    				winter [i] = doubletemp;
    			
    				doubletemp = spring [j];
    				spring [j] = spring [i];
    				spring [i] = doubletemp;
    				
    				doubletemp = summer [j];
    				summer [j] = summer [i];
    				summer [i] = doubletemp;
    				
    				doubletemp = total [j];
    				total [j] = total [i];
    				total [i] = doubletemp;
    			}
    		}
    	}
    }
    A small part of the datafile looks like this:
    Code:
    Hill Justin 2785.00 2282.00 5720.00 6330.00
    Ortego Taylor 4715.00 1676.00 6067.00 929.00
    Stoilova Nadica 1253.00 1495.00 2884.00 4173.00
    I don't understand the sorting concept in relation to the code.
    Please help!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    [Note: cross-posted. Replies have already been posted here.]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Standard C already has a great sorting function called qsort.

    Code:
    #include <stdlib.h>
    
    void qsort( void* base
                    , size_t nelem
                    , size_t size
                    , int (*cmp)(const void* e1, const void* e2)
                   );

    Hope that helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Separate long string into multiple arrays
    By cashmerelc in forum C Programming
    Replies: 6
    Last Post: 11-27-2007, 02:57 AM
  2. strings or arrays of characters?
    By Callith in forum C++ Programming
    Replies: 13
    Last Post: 12-26-2004, 11:28 AM
  3. Storing strings in 2d char arrays problem
    By rainmanddw in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2003, 05:41 PM
  4. strings or character arrays
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 10:55 AM
  5. Help With arrays and Sorting Strings
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 03-23-2002, 01:09 AM