954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Multidimensional array sort problem

Well this is my problem, I wrote a function to sort a 2d char array but right now the second dimension bound cannot be variable so it has to be hardset (which I don't particularly like)

This is what I have

void multi_charSort(char array[][5])
{
    int i,j;
    char temp2[5];
    for(i=0;i<5;i++)
    {
        for(j=0;j<i;j++)
        {
                    
             if((array[i][0]>='A' && array[i][0]<='Z') && (array[j][0]>='A' && array[j][0]<='Z'))
             {
                 if(array[i]>array[j])
                  {
                        for (int k=0;k<5;k++)
                        {
                            temp2[k]= array[i][k]; 
                            array[i][k]=array[j][k];
                            array[j][k]=temp2[k];
                        }
                  }
             }
             else if((array[i][0]>='a' && array[i][0]<='z') && (array[j][0]>='a' && array[j][0]<='z'))
             {
                  if(array[i]>array[j])
                  {
                        for (int k=0;k<5;k++)
                        {
                            temp2[k]= array[i][k]; 
                            array[i][k]=array[j][k];
                            array[j][k]=temp2[k];
                        }
                  }
             }   
       }
   }
}


Output:

2d char Array initialized!
Printing char Array:
1: Shaw
2: Tom
3: Bill
4: Adam
Sorting charArray!
1: Adam
2: Bill
3: Tom
4: Shaw



Any suggestions? (I'm also not completely sure if this is sorting correctly)

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Sorry ShawnCplus, but it looks like your code won't sort properly. At some places, you are comparing pointers to the strings instead of the characters in the strings. Try using a strcmp(array[i], array[j]) instead of array[i]>array[j] in the sort part (as this is not comparing the strings, but merely the pointers).

Do you really need to use a multidimensional array? It seems a bit wastefull of space and really inflexible (as you said, the bound must be hardset) - try using an array of char*. This allows you to have any length strings, and makes sorting faster (the swap function just swaps the pointers, not the actual data in the strings).

The following code does the job. Note that I have used a NULL pointer to indicate the end of my array of char pointers.

#include <stdio.h>
#include <conio.h>
#include <string.h>
 
using namespace std;
 
// bubble sort
void sort(char *st[]){
  for(int i = 0; st[i] != NULL; i++){
    for(int j = i+1; st[j] != NULL; j++){
      if(strcmp(st[i], st[j]) > 0){
        char *temp = st[i];    // swap pointers
        st[i] = st[j];
        st[j] = temp;
        }
      }
    }
  }
 
int main(){
  char *strings[] = {"Shawn", "Tom", "Bill", "Adam", NULL};   // using NULL to indicate last entry
 
  printf("Before sort:\n");
  for(int i = 0; strings[i] != NULL; i++)
    printf("\t%d: %s\n", i+1, strings[i]);
 
  sort(strings);
 
  printf("After sort:\n");
  for(int i = 0; strings[i] != NULL; i++)
    printf("\t%d: %s\n", i+1, strings[i]);
 
  getch();
  return 0;
  }


Output:

Before sort:
        1: Shawn
        2: Tom
        3: Bill
        4: Adam
After sort:
        1: Adam
        2: Bill
        3: Shawn
        4: Tom
dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

Yeah, I fixed it. The line that was actually checking the alphabetical order wasn't comparing the characters. And switching to a char* array forces me to use strcmp and the sub-point of it was to sort the array without any extra functions. But using NULL was fairly ingenious *thumbs up*. It's not like it really matters it was just something to do while I was bored. (Bad things happen when I'm bored... mainly to my computer)

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Not using strcmp because it's an "external function" is stupid (if this is an assignment, it's a horrible requirement). Especially when the external function doesn't relate to the problem (sorting).

I'd also point out that using char*, string.h, etc... are all from C.

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

> It's not like it really matters it was just something to do while I was bored.
I guess this explains it all. ;-)

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

strcmp does not have to be an external function. If library functions a problem, then write your own, e.g. :

char strcmp_(char *s1, char *s2){
  int i = -1;
  do{
    i++;
    if(s1[i] > s2[i])
      return 1;     // s1 larger
    else if(s1[i] < s2[i])
      return -1;      // s2 larger
    }while(s1[i] && s2[i]);
  return 0;   // equal
  }


But really, the library functions are there to use - of course all of them can be written again for every program you ever write, but what's the point? And by writing your own you introduce more possibilities for errors.

dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

dougy, if either one is NULL, you'll dereference it at least once in the loop body. It's little mistakes like this that make those library functions so important... :icon_wink:

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 
dougy, if either one is NULL, you'll dereference it at least once in the loop body. It's little mistakes like this that make those library functions so important... :icon_wink:



As I was saying...And by writing your own you introduce more possibilities for errors.

Yes you're right; but garbage in - garbage out. It should work for any valid string though.

dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You