Multidimensional array sort problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 1,423
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 229
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is online now Online
Code Monkey

Multidimensional array sort problem

 
0
  #1
Jul 2nd, 2007
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

  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:
  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.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Multidimensional array sort problem

 
0
  #2
Jul 2nd, 2007
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.

  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,423
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 229
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is online now Online
Code Monkey

Re: Multidimensional array sort problem

 
0
  #3
Jul 2nd, 2007
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)
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Multidimensional array sort problem

 
0
  #4
Jul 2nd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,647
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 473
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Multidimensional array sort problem

 
0
  #5
Jul 2nd, 2007
> It's not like it really matters it was just something to do while I was bored.
I guess this explains it all. ;-)
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Multidimensional array sort problem

 
0
  #6
Jul 3rd, 2007
strcmp does not have to be an external function. If library functions a problem, then write your own, e.g. :

  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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Multidimensional array sort problem

 
0
  #7
Jul 3rd, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Multidimensional array sort problem

 
0
  #8
Jul 3rd, 2007
Originally Posted by Infarction View Post
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...
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC