Fine tuning with SORT function using STRUC...真arhhhhh真
:confused: Hi all, im really getting confused (prolly confusing myself more than anything) with trying to get this function to work.
The idea is that Student ID's will be entered (Within the main) and then the function to sort them ascending, and obviously print them- (printing is obviously the easy part)
struct student {
char *id;
};
typedef struct student Student;
void sort( Student [] );
main () ...........
eg: Please enter first student ID: 00027270
'' '' second ID: 00028578
'' '' third ID: 00027896
int i;
Student student_id[ 3 ];
student_id[ 0 ].id = "00027270";
student_id[ 0 ].id = "00028578";
student_id[ 0 ].id = "00027896";
sort( all_student ); //call the function
//SORT function
void sort( Student s[] ) {
int i, counter;
Student hold;
for ( counter = 0; counter < 3 -1; counter++ ){
for( i = 0; i < 3 - counter - 1; i++ ){
if( strcmp( ( s + i )->first, (s + i + 1 )->first ) > 0 ) { // strcmp( s[ i ].first, s[ i + 1 ].first ) > 0
hold = s[ i ];
s[ i ] = s[ i + 1 ];
s[ i + 1 ] = hold;
}
}
}
}
with this part:--
( strcmp( ( s + i )->first, (s + i + 1 )->first ) > 0 )
??=> should this part continue on untill ninth (9) number
eg: (s + i + 2 )->first, (s + i + 3 )->first etc all the way untill 9.
or am i going about his the wrong way, like is their an easier way?
would u just have to reverse the coding to get them to sort in descending order.....
getting in over my head all, any bit of help would be soooooooooooo much appreciated, u have no idea
i wrote the wrong variable from main ()
:confused: HI, thanks for someone replying, :D
i did have a search throughout the C programming boards, and there wasnt much of a function that i coud relate to...
itsme86 asked :mad:
Quote
"EDIT: What's ->first? "
it was meant to be ->id (as im just trying to sort student id's)
i cant figure out if the strcmp needs anything more as im trying to sort 8 digit id's....
any more help would be greatly appreciated.
ps- this isn't homework (if it was id be right stuck in a hole) although yes it is an asignment (due pretty soon).... i thought i got so close to getting it done, but sorta cant do any more.