Skip to the task
Layout
Back to classic view

You are looking at lesson 10. Your place is still lesson 1, task 1: Algorithm.

EDITTask 7 of 17 · step 4: An array of citiesFive cities in an array
Five cities in an array in practice/10/

density, find_city with strcmp, sort_by_population (swaps whole structures) and at the end of main – the array, the list, the search.

Checklist

  • Every task is one commit with a clear message.
  • Zero compiler warnings: the code builds with -Werror.
  • Once per topic you step through your code in the debugger (CLion, gdb).
  • Every function has preconditions and postconditions, and assert tests check them.
  • A multi-file program: .h files with Doxygen comments and their own CMake target.
  • Input only through the safe functions of your own input.h.

Numbered notes in the code explain the important lines.

Details & hint
  • City temp = t[i]; swaps the whole structure, so the area stays with its city.
  • print_city(&cities[i]); is the address of the i-th element.
  • The earlier part of main doesn't change.
practice 10 cities.c Code Diff +56
M · MODIFIED Green = add, blue = change, red mark = removed line.
#include <assert.h>
#include <stdio.h>
#include <string.h>
#define MAX_NAME 20
#define CITY_COUNT 5
/* City: three pieces of data of different types under one name. */
typedef struct {
char name[MAX_NAME];
int population;
double area; /* km2 */
} City;
/* Prints one city. Before: c != NULL. */
void print_city(const City *c) {
assert(c != NULL);
printf("%s: %d people, %.1f km2\n", c->name, c->population, c->area);
}
/* Gets a COPY of the city: the original doesn't change. */
void grow_copy(City c, int count) {
c.population += count;
}
/* Gets a pointer to the city: changes the original. Before: c != NULL. */
void grow(City *c, int count) {
assert(c != NULL);
c->population += count;
}
/* Population density, people per km2. Before: c != NULL, c->area > 0. */
double density(const City *c) {
assert(c != NULL && c->area > 0);
Note 1: Dividing by 0 would make no sense – that's a precondition.
return c->population / c->area;
}
/* The index of the city with this name, or -1.
Before: n >= 0, t and name not NULL. */
int find_city(const City t[], int n, const char *name) {
assert(n >= 0 && t != NULL && name != NULL);
for (int i = 0; i < n; i++) {
if (strcmp(t[i].name, name) == 0) {
Note 2: Strings are compared with strcmp.
return i;
}
}
return -1;
}
/* Sorts by population, largest first, by selection.
Before: n >= 0, t != NULL. */
void sort_by_population(City t[], int n) {
assert(n >= 0 && t != NULL);
for (int i = 0; i < n - 1; i++) {
int max = i;
for (int j = i + 1; j < n; j++) {
if (t[j].population > t[max].population) {
Note 3: One member is compared...
max = j;
}
}
City temp = t[i];
Note 4: ...but the whole structure is swapped.
t[i] = t[max];
t[max] = temp;
}
}
int main(void) {
City kaunas = {"Kaunas", 300000, 157.0};
City vilnius = {.name = "Vilnius", .population = 600000, .area = 401.0};
print_city(&kaunas);
print_city(&vilnius);
printf("sizeof(City): %zu\n", sizeof(City));
City copy = kaunas;
copy.population = 0;
printf("kaunas: %d, copy: %d\n", kaunas.population, copy.population);
grow_copy(kaunas, 1000);
printf("After grow_copy: %d\n", kaunas.population);
grow(&kaunas, 1000);
printf("After grow: %d\n", kaunas.population);
City cities[CITY_COUNT] = {
Note 5: An array of structures with initial values.
{"Klaipeda", 150000, 98.0}, {"Panevezys", 85000, 50.0}, {"Vilnius", 600000, 401.0},
{"Alytus", 50000, 40.0}, {"Kaunas", 300000, 157.0},
};
sort_by_population(cities, CITY_COUNT);
printf("\nBy population:\n");
for (int i = 0; i < CITY_COUNT; i++) {
printf("%d. ", i + 1);
print_city(&cities[i]);
}
int k = find_city(cities, CITY_COUNT, "Alytus");
if (k != -1) {
printf("Alytus is in place %d, density %.0f people/km2\n", k + 1, density(&cities[k]));
}
if (find_city(cities, CITY_COUNT, "Utena") == -1) {
printf("Utena is not on the list.\n");
}
return 0;
}