Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct {
- char name[64];
- double height;
- double weight;
- } person;
- double bmi(person p) {
- double a = p.height / 100;
- return p.weight / (a * a);
- }
- void swap(person *p1, person *p2) {
- person p = *p1;
- *p1 = *p2;
- *p2 = p;
- }
- void sort_by_bmi(person *p, int n) {
- int k = n;
- while (k >= 0) {
- int j = -1;
- for (int i = 1; i <= k; i++) {
- if (bmi(p[i - 1]) > bmi(p[i])) {
- j = i - 1;
- swap(&p[j], &p[i]);
- }
- }
- k = j;
- }
- }
- int main(void) {
- person p[] = {
- { "一郎", 180.3, 77.1 },
- { "二郎", 188.0, 93.0 },
- { "三郎", 170.0, 63.0 },
- { "四郎", 171.8, 59.0 },
- { "五郎", 168.1, 56.5 },
- { "六郎", 173.7, 69.4 },
- { "七郎", 178.5, 60.3 },
- { "八郎", 175.2, 70.9 },
- { "九郎", 169.6, 55.7 },
- };
- sort_by_bmi(p, sizeof(p) / sizeof(person));
- for (int i = 0; i < sizeof(p) / sizeof(person); i++) {
- printf("%i: %s (%.1f)\n", i, p[i].name, bmi(p[i]));
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement