944,198 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6103
  • C++ RSS
Jul 2nd, 2007
0

Multidimensional array sort problem

Expand Post »
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

c++ Syntax (Toggle Plain Text)
  1. void multi_charSort(char array[][5])
  2. {
  3. int i,j;
  4. char temp2[5];
  5. for(i=0;i<5;i++)
  6. {
  7. for(j=0;j<i;j++)
  8. {
  9.  
  10. if((array[i][0]>='A' && array[i][0]<='Z') && (array[j][0]>='A' && array[j][0]<='Z'))
  11. {
  12. if(array[i]>array[j])
  13. {
  14. for (int k=0;k<5;k++)
  15. {
  16. temp2[k]= array[i][k];
  17. array[i][k]=array[j][k];
  18. array[j][k]=temp2[k];
  19. }
  20. }
  21. }
  22. else if((array[i][0]>='a' && array[i][0]<='z') && (array[j][0]>='a' && array[j][0]<='z'))
  23. {
  24. if(array[i]>array[j])
  25. {
  26. for (int k=0;k<5;k++)
  27. {
  28. temp2[k]= array[i][k];
  29. array[i][k]=array[j][k];
  30. array[j][k]=temp2[k];
  31. }
  32. }
  33. }
  34. }
  35. }
  36. }

Output:
C++ Syntax (Toggle Plain Text)
  1. 2d char Array initialized!
  2. Printing char Array:
  3. 1: Shaw
  4. 2: Tom
  5. 3: Bill
  6. 4: Adam
  7. Sorting charArray!
  8. 1: Adam
  9. 2: Bill
  10. 3: Tom
  11. 4: Shaw
Any suggestions? (I'm also not completely sure if this is sorting correctly)
Last edited by ShawnCplus; Jul 2nd, 2007 at 2:54 am.
Similar Threads
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jul 2nd, 2007
0

Re: Multidimensional array sort problem

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.

c++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6.  
  7. // bubble sort
  8. void sort(char *st[]){
  9. for(int i = 0; st[i] != NULL; i++){
  10. for(int j = i+1; st[j] != NULL; j++){
  11. if(strcmp(st[i], st[j]) > 0){
  12. char *temp = st[i]; // swap pointers
  13. st[i] = st[j];
  14. st[j] = temp;
  15. }
  16. }
  17. }
  18. }
  19.  
  20. int main(){
  21. char *strings[] = {"Shawn", "Tom", "Bill", "Adam", NULL}; // using NULL to indicate last entry
  22.  
  23. printf("Before sort:\n");
  24. for(int i = 0; strings[i] != NULL; i++)
  25. printf("\t%d: %s\n", i+1, strings[i]);
  26.  
  27. sort(strings);
  28.  
  29. printf("After sort:\n");
  30. for(int i = 0; strings[i] != NULL; i++)
  31. printf("\t%d: %s\n", i+1, strings[i]);
  32.  
  33. getch();
  34. return 0;
  35. }

Output:
C++ Syntax (Toggle Plain Text)
  1. Before sort:
  2. 1: Shawn
  3. 2: Tom
  4. 3: Bill
  5. 4: Adam
  6. After sort:
  7. 1: Adam
  8. 2: Bill
  9. 3: Shawn
  10. 4: Tom
Last edited by dougy83; Jul 2nd, 2007 at 4:48 am.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Jul 2nd, 2007
0

Re: Multidimensional array sort problem

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)
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jul 2nd, 2007
0

Re: Multidimensional array sort problem

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.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Jul 2nd, 2007
0

Re: Multidimensional array sort problem

> It's not like it really matters it was just something to do while I was bored.
I guess this explains it all. ;-)
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Jul 3rd, 2007
0

Re: Multidimensional array sort problem

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

c++ Syntax (Toggle Plain Text)
  1. char strcmp_(char *s1, char *s2){
  2. int i = -1;
  3. do{
  4. i++;
  5. if(s1[i] > s2[i])
  6. return 1; // s1 larger
  7. else if(s1[i] < s2[i])
  8. return -1; // s2 larger
  9. }while(s1[i] && s2[i]);
  10. return 0; // equal
  11. }

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.
Last edited by dougy83; Jul 3rd, 2007 at 3:26 am.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Jul 3rd, 2007
0

Re: Multidimensional array sort problem

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...
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Jul 3rd, 2007
0

Re: Multidimensional array sort problem

Click to Expand / Collapse  Quote originally posted by Infarction ...
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...
As I was saying...
Quote ...
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.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: designing classes
Next Thread in C++ Forum Timeline: interesting syntax





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC