Skip to the task
Layout
Back to classic view

You are looking ahead. Your place is still task 1: struct.

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 git diff: red = old code, green = new.
--- a/practice/10/cities.c
+++ b/practice/10/cities.c
@@ -1,7 +1,9 @@
#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 {
@@ -27,6 +29,41 @@
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);
+ 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) {
+ 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) {
+ max = j;
+ }
+ }
+ City temp = t[i];
+ 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};
@@ -42,5 +79,24 @@
printf("After grow_copy: %d\n", kaunas.population);
grow(&kaunas, 1000);
printf("After grow: %d\n", kaunas.population);
+
+ City cities[CITY_COUNT] = {
+ {"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;
}