Question

In C++ Define a class Country that stores the name of the country, its population, and...

In C++

Define a class Country that stores the name of the country, its population, and its area. Store the country name as a C-string. Write a main function that initializes an array with at least 5 objects of this class and sorts and prints this array in three different ways:

  • By population
  • By population density (people per square kilometer)

Do not ask the user to enter country data. Use qsort to sort the array.

  • Also sort alphabetically by country name, ignoring the case (upper-case or lower-case)
1 0
Add a comment Improve this question Transcribed image text
Answer #1

In case of any queries, please revert back.

I have explained everything in the comments step by step.

================= CODE BELOW ========================

#include <bits/stdc++.h>
using namespace std;
//declaring the class and the constructors.
// Empty constructor added for declaring array of objects.
class Country{
public:
long long int population;
long long int area;
char *name;
Country(){}
Country(long long int pop,long long int ar,char *nm){
name=nm;
population=pop;
area=ar;
}
};
int main()
{ //declaring a name array
char *nameArr[5];
nameArr[0]="A";
nameArr[1]="B";
nameArr[2]="C";
nameArr[3]="D";
nameArr[4]="E";
//declare list of objects of class Country
Country objList[5];
//provide different values to population and area
int pop=1,area=7;
for(int i=0;i<5;i++){
//declaring objects in array.
objList[i]=Country(pop,area,nameArr[i]);
pop=pop+30;
area=50;
}
//now we change the type of comparator used to sort objects. Here we check population
cout<<"============= sorting as per population =================\n";
sort(objList, objList + 5,
[](Country const & a, Country const & b) -> bool
{ return a.population < b.population; } );
for(int i=0;i<5;i++){
cout<<objList[i].population<<" "<<objList[i].name<<endl;
}
  
//now we change the type of comparator used to sort objects. Here we check population/area
cout<<"============= sorting as per population density =================\n";
sort(objList, objList + 5,
[](Country const & a, Country const & b) -> bool
{ return (a.population/a.area) < (b.population/b.area); } );
for(int i=0;i<5;i++){
cout<<objList[i].population<<" "<<objList[i].name<<endl;
}
  
//now we change the type of comparator used to sort objects. Here we check name via strcmp operation.
cout<<"============= sorting as per population density =================\n";
sort(objList, objList + 5,
[](Country const & a, Country const & b) -> bool
{ int x=strcmp(a.name,b.name);
if(x>0) return true;
return false; } );
for(int i=0;i<5;i++){
cout<<objList[i].population<<" "<<objList[i].name<<endl;
}
return 0;
}

=================== SCREENSHOTS BELOW =====================

Add a comment
Know the answer?
Add Answer to:
In C++ Define a class Country that stores the name of the country, its population, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++. Define a class Country that stores the name of the country, its population, and...

    In C++. Define a class Country that stores the name of the country, its population, and its area. Store the country name as a C-string. Write a main function that initializes an array with at least 5 objects of this class and sorts and prints this array in three different ways: By population By population density (people per square kilometer) Do not ask the user to enter country data. Use qsort to sort the array. Also sort alphabetically by country...

  • Write the program in C The following statistics for cities of the Vege country are stored...

    Write the program in C The following statistics for cities of the Vege country are stored in a file: Name: a string of characters less than 15 characters long. Population: an integer value. Area: square miles of type double Write a program that: asks and gets the name of the input file from the user. Read the contents of the file into an array length 10 of structures -ask the user for the name of a city (less than 15...

  • In Java, define classes for a LikeYelp class that manages reviews about stores. You need to...

    In Java, define classes for a LikeYelp class that manages reviews about stores. You need to fill in the LikeYelp class and outline supporting classes. For the supporting classes, you need to provide the signatures for the methods and enough instance variables to support the methods, but you do not need to write the code for the methods. You need to write the entire LikeYelp class so it supports the following Add a store, which has a name and an...

  • Create a C program to implement a grade book. Must follow these guidelines: 1. Use #define...

    Create a C program to implement a grade book. Must follow these guidelines: 1. Use #define to define MAX_SIZE1 as 20, and MAX_SIZE2 as 10 2. Use typedef to define the following struct type: struct { char name[MAX_SIZE1]; int scores[MAX_SIZE2]; } 3. The program accepts from the comand line an integer n (<= 30) as the number of students in the class. You may assume that the input will always be valid. 4. Dynamically allocate memory for an array of...

  • Class River describes river’s name and its length in miles. It provides accessor methods (getters) for...

    Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables and toString() method that returns String representation of the river. Class CTRivers describes collection of CT rivers. It has no data, and it provides the following service methods. None of the methods prints anything, except method printListRec, which prints all rivers. // Prints all rivers recursively. Print them is same order as they were in the list . List can be empy...

  • The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements...

    The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements both the selection sort and the insertion sort algorithms for sorting any array of Comparable objects in ascending order. In this exercise, you will use the Sorting class to sort several different types of objects. 1. The file Numbers.java reads in an array of integers, invokes the selection sort algorithm to sort them, and then prints the sorted array. Save Sorting.java and Numbers.java to...

  • Please help. I need a very simple code. For a CS 1 class. Write a program...

    Please help. I need a very simple code. For a CS 1 class. Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "printall" - prints all student records (first name, last name, grade). "firstname name" - prints all students with...

  • Task 01: (a)Write a class Fruit with: private instance variables name, and pricePerKilogram Appropriate constructor Appropriate...

    Task 01: (a)Write a class Fruit with: private instance variables name, and pricePerKilogram Appropriate constructor Appropriate accessor methods Appropriate mutator methods toString method equals method (b) Write a FruitDriver class that: Initializes an array of Fruit objects, fruitArray, with 10 objects with names: banana, apple, mango, orange,pineapple, pear, grapes, tangerine, watermelon, sweetmelon and appropriate prices per kilogram. Uses an appropriate loop to display all objects with pricePerKilogram > 5.00 Saudi Riyals, if any. Calls a linearSearch method:                        public static...

  • In c++ show both .cpp and .hpp file for the class: class Task { private: //...

    In c++ show both .cpp and .hpp file for the class: class Task { private: // These three member variables store the basic information about a task string title; string location; // This pointer will point to a dynamically-allocated array // of THREE strings. Each element contains the name of a supply required for this task. // This should start out set to NULL. string * tags; // This integer stores the number of elements in the tags array. int...

  • Define a structure called Date with month, day and year as its only data members, Time...

    Define a structure called Date with month, day and year as its only data members, Time with hour and minute as its only members and Event with desc (a c-string for description of maximum size 80), date (of type Date) and time (of type Time). Write a readEvents() function that will keep asking the user if he or she wants to add a new event. As long as the user answers with a 'y' or 'Y', allocate memory dynamically for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT