Discover a wealth of knowledge at Westonci.ca, where experts provide answers to your most pressing questions. Experience the convenience of finding accurate answers to your questions from knowledgeable experts on our platform. Explore comprehensive solutions to your questions from a wide range of professionals on our user-friendly platform.

Define a class Country that stores the name of the country, its population, and its area. Using that class, write a program that reads in a set of countries and prints

ļ‚· The country with the largest area
ļ‚· The country with the largest population
ļ‚· The country with the largest population density (people per square kilometer)

Sagot :

Answer:

here hope this helps.

Explanation:

#include <iostream>

using namespace std;

//Country class declaration

class Country {

public:

string country;

double population;

double area;

};

int main()

{

Country ctry[100];

int n;

double large_population, large_area, large_density, temp_density;

string p_ctry, a_ctry, d_ctry;

 

// Reading input on how many countries we want to store the information

cout << "How many countries you want to read the data ? ";

cin >> n;

// Reading country name, population and area for the countries

for(int i = 0; i < n; i++) {

cout << "Enter Country Name: ";

cin >> ctry[i].country;

cout << "Enter population: ";

cin >> ctry[i].population;

cout << "Enter area: ";

cin >> ctry[i].area;

}

 

large_population = 0;

large_area = 0;

large_density = 0;

// Identifying largest population and area country

for(int i = 0; i < n; i++) {

if(ctry[i].population > large_population) {

p_ctry = ctry[i].country;

large_population = ctry[i].population;

}

if(ctry[i].area > large_area) {

a_ctry = ctry[i].country;

large_area = ctry[i].area;

}

// Population density is population divided by total land area or water volume

temp_density = ctry[i].population / ctry[i].area;

if(temp_density > large_density) {

d_ctry = ctry[i].country;

large_density = temp_density;

}

}

 

cout << endl;

cout << "Country "<< a_ctry << " has the largest area " << large_area << endl;

cout << "Country "<< p_ctry << " has the largest population " << large_population << endl;

cout << "Country "<< d_ctry << " has the largest population density " << large_density << endl;

return 0;

}