![]() |
| | #1 |
| Registered User Join Date: May 2009
Posts: 1
| i'm trying to sort an array of string. my code should sort my array alphabetically [a-z] ,but it dos not do that .. Code: int main(void)
{
char mov[7][25];
int f,c,i; ///first , current , i-->char index 0-24;
int tmp,save[7]={0,1,2,3,4,5,6};
i=23;
strcpy(mov[0],"Cheaper by the Dozen 2");
strcpy(mov[1],"Drumline");
strcpy(mov[2],"Ever After");
strcpy(mov[3],"Cheaper by the Dozen");
strcpy(mov[4],"Mr. & Mrs. Smith");
strcpy(mov[5],"X-Men");
strcpy(mov[6],"Toy Story 2");
puts("<----before---->");
for(i=0;i<7;i++)puts(mov[save[i]]);
puts("<--------------->");
while(i>=0)
{
for(f=0;f<6;f++)
for(c=f+1;c<7;c++)
{
if(mov[f][i]<mov[c][i])
{
tmp=save[f];
save[f]=save[c];
save[c]=tmp;
}
}
i--;
}
puts("<-----after----->");
for(i=0;i<7;i++)puts(mov[save[i]]);
puts("<--------------->");
return 0;
}
<----before----> Cheaper by the Dozen 2 Drumline Ever After Cheaper by the Dozen Mr. & Mrs. Smith X-Men Toy Story 2 <---------------> <-----after-----> Cheaper by the Dozen 2 Drumline Mr. & Mrs. Smith Ever After X-Men Cheaper by the Dozen Toy Story 2 <---------------> as you see in the output the string is not sorted .. i think, I'm missing something basic here... |
| Alexi_n is offline | |
| | #2 |
| Registered User Join Date: Nov 2004 Location: india
Posts: 493
| The order of your loops is jumbled up. I am not going to give you the code, but write some pseudocode which you should be able to follow Code: for i in range(0, numberofstrings)
for j in range(0, numberofstrings-i-1)
string1=arr[j]
string2=arr[j+1]
for k in range(0, min(size(string1), size(string2)))
if string1[k] > string2[k]
swap string1 and string2 and break
else if string1[k] < string2[k]
break
|
| PING is offline | |
| | #3 |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,654
| Look at the way you're comparing items. For example, take just these two strings: Code: Drumline Ever After Then you're comparing the second chars r and v and again you make Drumline come first because r is less than v. Then you're comparing the third chars u and e and now you make Ever After come first because e is less than u - WTF! That's not how you order strings. Once you're compared the first characters and they are not the same then the rest of the characters are not relevant. Of course that isn't the entire extent of the problems with that code, by far.
__________________ My homepage Advice: Take only as directed - If symptoms persist, please see your debugger |
| iMalc is offline | |
| | #4 |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,654
| Oh no wait, that outer loop is going backwards, and from only 7. It's actually not doing what I just said - It's far worse! This code is stretching the limits of just how wrongly something can be done...
__________________ My homepage Advice: Take only as directed - If symptoms persist, please see your debugger |
| iMalc is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| allocation and reallocation of memory dynamically of an integer array | zamir | C++ Programming | 16 | 05-29-2009 07:25 PM |
| [question]Analyzing data in a two-dimensional array | burbose | C Programming | 2 | 06-13-2005 07:31 AM |
| Unknown Memory Leak in Init() Function | CodeHacker | Windows Programming | 3 | 07-09-2004 09:54 AM |
| Quick question about SIGSEGV | Cikotic | C Programming | 30 | 07-01-2004 07:48 PM |
| Array Program | emmx | C Programming | 3 | 08-31-2003 12:44 AM |