Answer to Question #162144 in C++ for waleed

Question #162144

Write a C++ program that declare a structure “Country” having cid, cname as a members. Declare another structure “Cities” having member’s citiy_name, male population, and female population also contains country structure as a reference. Input the record of 4 cities in structure variable and passes them to a function as an argument. The function will display all record of city having highest male and female population.


1
Expert's answer
2021-02-08T15:26:52-0500
#include <iostream>
using namespace std;

struct Country
{
    int cid;
    string name;
};

struct Cities
{
    string city_name;
    int male_population;
    int female_population;
    struct Country *country;
};

void DisplayMax(Cities cities[4])
{
    Cities max_city = cities[0];
    int max_pop = cities[0].male_population + cities[0].female_population;
    for(int i = 1; i < 4; i++)
    {
        if(max_pop < cities[i].male_population + cities[i].female_population)
        {
            max_pop = cities[i].male_population + cities[i].female_population;
            max_city = cities[i];
        }
    }
    cout<<"The city with the highest male and female population is:\n";
    cout<<"Name: "<<max_city.city_name<<endl;
    cout<<"Male population: "<<max_city.male_population<<endl;
    cout<<"Female population: "<<max_city.female_population<<endl;
    cout<<"Country name: "<<(*max_city.country).name<<endl;
    cout<<"cid: "<<(*max_city.country).cid<<endl;
}

int main()
{
    Country Kenya = {254, "Kenya"};
    Country USA = {001, "USA"};
    Country UK = {44, "United Kingdom"};
    Country France = {33, "France"};

    Cities Nairobi = {"Nairobi", 2192452, 2204376, &Kenya};
    Cities Washington = {"Washington DC", 334355, 371394, &USA};
    Cities London = {"London", 4486000, 4496000, &UK};
    Cities Paris = {"Paris", 1101500, 1146000, &France};

    Cities cities[] = {Nairobi, Washington, London, Paris};

    DisplayMax(cities);

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment