Thread: Sort and compare 2D Array

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    3

    Question Sort and compare 2D Array

    DISCLAIMER: 1st post, please excuse any format violations...
    Good evening folks.

    Here's the deal. I have seven arrays to compare and sort in alphabetical order. My problem is this...

    Input
    box
    fat
    toy
    red

    All three letters (individually) are being sorted, so that it would look like this...
    toy
    rox
    fet
    bad

    My goal is to sort them alphabetically a------>z (as whole words).

    Any suggestions? Thanks for the help!

    Code:
    #include<stdio.h>
    #include<string.h>
    
    #define N_STRINGS 7
    #define MAX_SIZE 80
    
    void sort(char a[][MAX_SIZE], int n);
     
    int main(void)
    {
          int i, n=N_STRINGS;
          char a[N_STRINGS][MAX_SIZE];
    
          printf("Enter 7 single character strings.\n\n");      
          for(i=0; i<n; ++i)
            scanf("%s",&a[i]);
    
          printf("The Strings you have entered:");//print the list of string characters
          for(i = 0; i<n; ++i)
            printf("%s\n",&a[i]);
       
          sort(a, n);//sort um   
    
    
          printf("\nThe Stings sorted are:\n");//print the list of string characters
          for(i = 0; i<n; ++i)
            printf("%s\n",&a[i]);
          return 0; 
    }
    
     void sort(char a[][MAX_SIZE], int n)
    {
         int i, j, k;
         char temp;
    	
         for (i = 0; i < n; i++)		{
              for (j = i + 1; j < n; j++)		{
    	for (k = 0; k < n; k++)		{ 
    	     if (strcmp(&a[j][k],&a[i][k])>0)	{
    		temp = a[i][k];
    		a[i][k] = a[j][k];
    		a[j][k] = temp;
    	     }
                   } 
             }
        }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, since you're using string functions already, why don't you use strcmp, and strcpy to first compare entire words, and then copy them to the correct place?

    1) Copy word N to a temporary array.
    2) Copy word X to where word N is in the array.
    3) Copy temporary array to where word X is in the array.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can't use string comparison if you want to do individual letters. You should loop through a bit differently. Maybe something like:
    Code:
    for(i = 0;a[0][i];i++)
      for(j = 0;j < n;j++)
        for(k = j+1;k < n;k++)
          if(a[j][i] < a[k][i])
            // Swap characters
    That's assuming all the strings are the same length.

    EDIT: Nevermind. I thought you wanted to have them sorted all weird and was having problems with it. fet is a cool word.
    Last edited by itsme86; 01-20-2005 at 10:41 AM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > if (strcmp(&a[j][k],&a[i][k])>0) {
    As Quzah said, what you want here is:
    if (strcmp(a[j],a[i])>0) {

    Then use strcpy() to swap a[i] and a[j]. And you don't need the k loop. Finally temp should be a char array.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    3

    Thumbs up

    Thanks for the advice! I'm up and running now...

    I went with the strcpy route which seems to work well, however, how can you make it tolerate strings of different lengths, when you overwrite one array with another using the copy function?

    Ex copy red to blue = blue
    copy blue to yellow = blueow

    Code:
    void sort(char a[][MAX_SIZE], int n)			
    {
         int i, j;		
         char temp[1][MAX_SIZE];	
    	
         for (i = 0; i < n; i++)		{
              for (j = i + 1; j < n; j++)		{
    	if (strcmp(a[i], a[j]) > 0)	{
    	     strcpy(temp[i], a[i]);
    	     strcpy(a[i], a[j]);
    	     strcpy(a[j], temp[i]);

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    There's no need for temp to be 2-dimensional.
    Code:
    void sort(char a[][MAX_SIZE], int n)			
    {
         int i, j;		
         char temp[MAX_SIZE];	
    	
         for (i = 0; i < n; i++)		{
              for (j = i + 1; j < n; j++)		{
    	if (strcmp(a[i], a[j]) > 0)	{
    	     strcpy(temp, a[i]);
    	     strcpy(a[i], a[j]);
    	     strcpy(a[j], temp);

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >how can you make it tolerate strings of different lengths
    Your strings should already be terminated with a string terminator('\0'), so this shouldn't be a problem unless you are building each string char-by-char, in which case you should add the string terminator yourself.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    3

    Talking

    I guess this was one of those situations where I was over-thinking this whole deal.

    Thanks alot!

    It's up and running, with problems <= 0

    Whoohoo!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. How to sort an array of pointers to structure
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 06-30-2008, 02:52 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM