Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <conio.h>
- #include <stdlib.h>
- #define NO_OF_STUDENTS 4
- struct data {
- char name[20];
- int age;
- int weight;
- };
- int compare (const void * a, const void * b);
- int main()
- {
- struct data Students[NO_OF_STUDENTS]; // 20 students.
- int Counter = 0;
- char temp[20];
- int i,j;
- for (Counter = 0; Counter < NO_OF_STUDENTS; ++Counter) {
- scanf("%s", Students[Counter].name);
- scanf("%d", &Students[Counter].age);
- scanf("%d", &Students[Counter].weight);
- }
- for (Counter = 0; Counter < NO_OF_STUDENTS; ++Counter) {
- printf("Name=%s, ", Students[Counter].name);
- printf("Age=%d, ", Students[Counter].age);
- printf("Weight=%d\n", Students[Counter].weight);
- }
- qsort(Students,4,sizeof(int),compare);
- for(i=1;i<5;i++)
- {
- for(j=1;j<5;j++)
- {
- if(strcmp(Students[j-1].name,Students[j].name)>0)
- {
- strcpy(temp,Students[j-1].name);
- strcpy(Students[j-1].name,Students[j].name);
- strcpy(Students[j].name,temp);
- }
- }
- }
- for (Counter = 0; Counter < NO_OF_STUDENTS; ++Counter) {
- printf("Name=%s, ", Students[Counter].name);
- printf("Age=%d, ", Students[Counter].age);
- printf("Weight=%d\n", Students[Counter].weight);
- }
- return 0;
- }
- int compare (const void * a, const void * b)
- {
- return ( *(int*)a - *(int*)b );
- }
Advertisement
Add Comment
Please, Sign In to add comment