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)

Recommended Answers

All 7 Replies

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, array[j]) instead of array>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

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)

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.

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

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.

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:

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.