BK11022
BS11333
BK11111
BH46222
how do i sort this kind of thing?i treat it as a string?i got try to sort the alphabet thing but the number behide wont be sorted help~!!!
BK11022
BS11333
BK11111
BH46222
how do i sort this kind of thing?i treat it as a string?i got try to sort the alphabet thing but the number behide wont be sorted help~!!!
Last edited by archriku; 03-26-2009 at 12:40 PM.
Numbers can be sorted as part of a string:
char numbers[10][10];
char letters[10][10];
are both strings if the last char in each entry is the end of string char: '\0'.
Without that end of string char, each entry would be a collection of char's, but it wouldn't be a string.
Are you ready for something a little blow your mind ish?
Usually, you think of sorting as moving things into sorted order, but there is another way to sort things that doesn't move anything except a single number, and I'd recommend it for you, with these structs.
Why? Because otherwise you have to move every members of each struct that has to be moved, with every swap you make.
It's called "sorting by Index" and here's how it works:
You make up a small array of integers, the same size as the array you want to sort.
Then you set it up with 1,2,3,4... in order, numbers.
Then you look at your items, and see which one's are out of order, and then you swap - but *not* the items you want sorted - you swap the index number only.
When you want to see the sorted order, you print or send the output to a file, *using the Index*.
Pretty spiffy, eh?Code://initialize the Index array: for(i = 0; i < MaxSoups; i++) Index[i] = i; /* Soup Cans to be Sorted Index before sorting Index after sorting ======================================================================== Chicken Soup 1 1 Vegetable Soup 2 4 Rice with Chicken Soup 3 5 Clear Broth 4 3 Noodles 5 2 */ //Now sort the Soups by Name: for(i = 0; i < MaxSoups - 1; i++) { for(j = i + 1; j < MaxSoups; j++) { if(strcmp(Soups[i], Soups[j]) > 0) { temp = Index[i]; Index[i] = Index[j]; Index[j] = temp; } } } //Now show the soups in sorted order: for(i = 0; i < MaxSoups; i++) printf("%s \n", Soups[Index[i]]);
Not to worry about the numbers. The telephone book has been sorting numbers for years, and we can, also.
Vegetable Soup was #2 on the list, and after sorting, it's #5: (of course subtract 1 for C because arrays in C start with zero, not one)
Last edited by Adak; 03-26-2009 at 05:24 AM.
Numbers sort out lower than letters - and that's how it should be done.
12345
99999
AAAAA
aaaaa
Should be your order for the strings shown here. Numbers before letters, Uppercase letters before lowercase.
fCode:or(i = 0; i < MaxSoups - 1; i++) { for(j = i + 1; j < MaxSoups; j++) { if(strcmp(Soups[i], Soups[j]) > 0) { temp = Index[i]; Index[i] = Index[j]; Index[j] = temp; }
can u please briefly explain what is the temp, i, j variable is? what they represent?
i think i is the number of element of the soup, Maxsoup - 1 becuz array starts with 0, ermm i think j is the second element after i since it is i + 1 so if i is the 1st soup then j is the second soup.. am i correct?but what is temp?i really dun have any idea about this
oh yea does izzit the Maxsoup must be subtitute by a number?
Last edited by archriku; 03-26-2009 at 05:55 AM.
So, i is the index into the array called index - we pass over ALL soups. j is used to work over the "remaining" soups - from the (i+1) to max soups - that way.
If the condition of strcmp() is true, we have found a soup that is in the wrong order. So we need to swap them.
Now, imagine that you and a friend is carrying numbers. Numbers are so heavy that you can only hold one. You are standing next to your friend, but the numbers are in the wrong order, so you need to swap with your friend. But neither of you can hold two numbers at once. How are you going to solve it? You need a second friend. Let's call him temp - temp holds your number, you take your friends number, and temp gives your number to your friend. That is how the swap works inside the if-statement.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
ic
so lets have i have 4 matrix number BK12345 BK54321 BX12345 BX54321
i declare them asso i do my sorting code lidis?Code:char matrix[10], temp;
but then Adak said the BK123123 in 2D array matrix[10][10]Code:for ( i = 0; i < 4; i++); for( j = i + 1 ; j < 5; j++); { if(strcpm(matrix[i],matrix j)>0) { temp = matrix[i] matrix[i] = matrix[j] matrix[j] = temp }
so how i write for the matrix[i]?
A string "BK12345" is indeed a character array in itself, and you could not fit that in a single 10 character array like you declare in your post.
Also, for copying strings, you need to use strcpy(), otherwise it won't work.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Yes, your strcpy() looks right. Of course, temp needs to be the same size as the second size in matrix.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.