SHOW:
|
|
- or go back to the newest paste.
| 1 | - | // #include "stdafx.h" crap not needed |
| 1 | + | |
| 2 | #include <string.h> | |
| 3 | #include <conio.h> | |
| 4 | - | // #include <conio.h> crap not needed |
| 4 | + | |
| 5 | ||
| 6 | ||
| 7 | #define NO_OF_STUDENTS 4 | |
| 8 | - | #define NO_OF_STUDENTS 4 // too lazy for 20 |
| 8 | + | |
| 9 | struct data {
| |
| 10 | char name[20]; | |
| 11 | int age; | |
| 12 | int weight; | |
| 13 | }; | |
| 14 | ||
| 15 | ||
| 16 | int compare (const void * a, const void * b); | |
| 17 | ||
| 18 | ||
| 19 | int main() | |
| 20 | {
| |
| 21 | struct data Students[NO_OF_STUDENTS]; // 20 students. | |
| 22 | int Counter = 0; | |
| 23 | char temp[20]; | |
| 24 | int i,j; | |
| 25 | for (Counter = 0; Counter < NO_OF_STUDENTS; ++Counter) {
| |
| 26 | scanf("%s", Students[Counter].name);
| |
| 27 | scanf("%d", &Students[Counter].age);
| |
| 28 | scanf("%d", &Students[Counter].weight);
| |
| 29 | } | |
| 30 | ||
| 31 | for (Counter = 0; Counter < NO_OF_STUDENTS; ++Counter) {
| |
| 32 | - | |
| 32 | + | |
| 33 | - | |
| 33 | + | |
| 34 | - | |
| 34 | + | |
| 35 | - | |
| 35 | + | |
| 36 | - | |
| 36 | + | |
| 37 | - | |
| 37 | + | |
| 38 | - | |
| 38 | + | |
| 39 | - | |
| 39 | + | |
| 40 | - | |
| 40 | + | |
| 41 | ||
| 42 | ||
| 43 | ||
| 44 | ||
| 45 | qsort(Students,4,sizeof(int),compare); | |
| 46 | ||
| 47 | ||
| 48 | - | |
| 48 | + | for(i=1;i<5;i++) |
| 49 | - | if(strcmp(Students,NO_OF_STUDENTS)){
|
| 49 | + | {
|
| 50 | - | |
| 50 | + | for(j=1;j<5;j++) |
| 51 | - | printf("Name=%s, ", Students[Counter].name);
|
| 51 | + | {
|
| 52 | - | |
| 52 | + | if(strcmp(Students[j-1].name,Students[j].name)>0) |
| 53 | - | |
| 53 | + | {
|
| 54 | strcpy(temp,Students[j-1].name); | |
| 55 | - | |
| 55 | + | strcpy(Students[j-1].name,Students[j].name); |
| 56 | - | |
| 56 | + | strcpy(Students[j].name,temp); |
| 57 | - | |
| 57 | + | } |
| 58 | } | |
| 59 | - | |
| 59 | + | } |
| 60 | - | |
| 60 | + | |
| 61 | ||
| 62 | ||
| 63 | ||
| 64 | for (Counter = 0; Counter < NO_OF_STUDENTS; ++Counter) {
| |
| 65 | printf("Name=%s, ", Students[Counter].name);
| |
| 66 | printf("Age=%d, ", Students[Counter].age);
| |
| 67 | printf("Weight=%d\n", Students[Counter].weight);
| |
| 68 | } | |
| 69 | ||
| 70 | ||
| 71 | ||
| 72 | ||
| 73 | return 0; | |
| 74 | ||
| 75 | ||
| 76 | } | |
| 77 | ||
| 78 | ||
| 79 | ||
| 80 | ||
| 81 | ||
| 82 | int compare (const void * a, const void * b) | |
| 83 | {
| |
| 84 | return ( *(int*)a - *(int*)b ); | |
| 85 | } |