-
Notifications
You must be signed in to change notification settings - Fork 14
/
12.c
102 lines (93 loc) · 2.08 KB
/
12.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <string.h>
struct census
{
char city[20];
long int population;
float literacy;
};
void display_city(struct census city[])
{
int i;
printf("The details of the cities are:\n");
printf(" City\t\tPopulation\tLiteracy rate\n");
for (i = 0; i < 5; i++)
{
printf(" %s\t%ld\t\t%f\n", city[i].city, city[i].population, city[i].literacy);
}
}
void sort_list_alphabetically(struct census city[])
{
int i, j;
struct census temp;
for (i = 0; i < 5; i++)
{
for (j = i; j < 5; j++)
{
if (strcmp(city[i].city, city[j].city) > 0)
{
temp = city[i];
city[i] = city[j];
city[j] = temp;
}
}
}
display_city(city);
}
void sort_list_by_population(struct census city[])
{
int i, j;
struct census temp;
for (i = 0; i < 5; i++)
{
for (j = i; j < 5; j++)
{
if (city[i].population > city[j].population)
{
temp = city[i];
city[i] = city[j];
city[j] = temp;
}
}
}
display_city(city);
}
void sort_list_by_literacy(struct census city[])
{
int i, j;
struct census temp;
for (i = 0; i < 5; i++)
{
for (j = i; j < 5; j++)
{
if (city[i].literacy > city[j].literacy)
{
temp = city[i];
city[i] = city[j];
city[j] = temp;
}
}
}
display_city(city);
}
int main()
{
struct census city[5];
int i;
for (i = 0; i < 5; i++)
{
printf("Enter the name of the city: ");
scanf("%s", city[i].city);
printf("Enter the population of the city: ");
scanf("%ld", &city[i].population);
printf("Enter the literacy rate of the city: ");
scanf("%f", &city[i].literacy);
}
printf("\n");
sort_list_alphabetically(city);
printf("\n");
sort_list_by_literacy(city);
printf("\n");
sort_list_by_population(city);
return 0;
}