# Thread: Fine tuning with SORT function using STRUC...真arhhhhh真

1. ## Fine tuning with SORT function using STRUC...真arhhhhh真

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

2. This reeks of homework.

Use [ code ][ /code ] tags to post code.

EDIT: What's ->first?

3. A board search for qsort would probably throw up plenty of examples

4. ## i wrote the wrong variable from main ()

i did have a search throughout the C programming boards, and there wasnt much of a function that i coud relate to...

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.

5. ps i will be posting the updated code in a couple of hours
ta

6. If you don't have to use strcmp() you could simply store each ID as a long int and then do sorting by < and > operations, which would probably be much faster.

7. Well, my apologies for making the homework assumption. Is this what you're looking for?

Code:
```#include <stdio.h>
#include <stdlib.h>

typedef struct
{
int id;
} Student;

void sort(Student *list, int num)
{
Student *outer, *inner;
Student temp;

for(outer = list;outer-list < num;++outer)
for(inner = outer+1;inner-list < num;++inner)
if(outer->id > inner->id)
{
temp = *outer;
*outer = *inner;
*inner = temp;
}
}

int main(void)
{
Student *list = NULL, *ptr;
int n_students = 0;
char buf[256];

puts("Enter IDs one per line. Blank line to end.");
for(;;)
{
printf("ID %d: ", n_students+1);
fflush(stdout);
fgets(buf, sizeof(buf), stdin);
if(*buf == '\n')
break;
if(!(ptr = realloc(list, sizeof(Student)*(n_students+1))))
{
puts("Memory allocation error. Processing existing IDs...");
break;
}
list = ptr;
list[n_students++].id = atoi(buf);
}

sort(list, n_students);

for(ptr = list;ptr-list < n_students;++ptr)
printf("%08d\n", ptr->id);

free(list);
return 0;
}```
itsme@dreams:~/C\$ ./idsort
Enter IDs one per line. Blank line to end.
ID 1: 27270
ID 2: 28578
ID 3: 27896
ID 4:
00027270
00027896
00028578
itsme@dreams:~/C\$

8. itsme86, dude U RocK!
Qutoe: "<Is this what you're looking for?>" .... X-actly

thanks for that, i was wasnt expecting so much, but this is great,
This program is going to eventually have a alot more Struct's with several more functions..... i can incorporate this code for lots of other tasks that i have planned

once agian thanks itsme

9. 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.
Ok, wait a second. Do you even know the definition of home work? An assignment "due pretty soon" counts in my book as homework. Don't pretend it isn't.

I don't know even why I bother any more. Everyone just does all the work for people, none of the new people read any of the Announcements, use code tags or anything else like they're supposed to. God damn I'm getting tired of people.

Quzah.

10. I don't blame you quzah. It's all in the art of handouts.

Help gears toward improvement and direction, not result or solution. I believe the difference is weighed greatly.

11. Well I guess I'm just the kind of person that, when I receive help in the way I give it, I try my best to understand how and why the author did it the way they did. And if I can't understand it then I ask specifics. It's easier for me to see the whole picture and then try to understand little pieces of it instead of getting told how to do little pieces and then mutate it into what I want.

But that's just me and everybody's different. Doesn't mean I'm right, doesn't mean you're right. If there's a well-defined policy here that says not to give code in full then I'll stop doing it. Until I see something like that then I'll help people the way that I think is best. If my help isn't welcome then I'll stop posting.