Programming contests

50 Programming Exercise for Beginners

January 2, 2019, 12:00 AM – December 31, 2019, 12:00 AM

High-Density Countries

Write a program that reads data of countries from the input file input.txt until end-of-file, one country per line. Each line has the following structure:

name;area;population[;population]…

name is a string with no more than 30 characters, containing no semicolons, area and population are positive integers. The total population of the country is the sum of the population values in the line. The file consists of up to 200 lines, each at most 1000 characters long. The program should write to the standard output the name, area, and total population of those countries whose population density (population to area ratio) exceeds the real value given in the first command line argument of the program, one country per line. Two consecutive data values in a line should be separated by a comma. Sort the output in descending order of population densities. Countries with the same population density should be sorted in ascending order of country names.

Hint: see atoi(), atof(), or sscanf() functions for converting a string to an integer or a real number, see the strtok() function for tokenizing a string, and see the qsort() function for sorting.

Sample Command Line Arguments

  1. prog 100
download as text file

Sample input.txt

  1. Magyarorszag;93030;4851354;5042545
  2. Amerikai Egyesult Allamok;9826630;10000000;20000000;30000000;40000000;50000000;60000000;70000000;38697314
  3. Nemetorszag;357023;80500000
  4. Kanada;9984670;10000000;20000000;5702707
  5. Egyesult Kiralysag;244820;30000000;33705000
download as text file

Output for Sample Input

  1. Egyesult Kiralysag,244820,63705000
  2. Nemetorszag,357023,80500000
  3. Magyarorszag,93030,9893899
download as text file
Acknowledgement This work was supported by the construction EFOP-3.4.3-16-2016-00021. The project was supported by the European Union, co-financed by the European Social Fund.
University of Debrecen; Faculty of Informatics; v. 09/30/2024