1 answer

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

Question:

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)

Answers

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 =====================

.

Similar Solved Questions

1 answer
Please solve the following question with proper steps .true or fa level curves of fx.y For 18-21circle true or false for each statement, False. True or The graph of y x in R is a cylinder 18. true...
Please solve the following question with proper steps .true or fa level curves of fx.y For 18-21circle true or false for each statement, False. True or The graph of y x in R is a cylinder 18. true false If a particle is moving along a space curve at constant speed, then a must be o 19. true false 20...
1 answer
Renal 3. Develop at least one Nursing Care plan for Chronic Renal Failure
Renal 3. Develop at least one Nursing Care plan for Chronic Renal Failure...
1 answer
28-8: Future Value of an Ordinary Annuity Problem 28-24 Required Lump-Sum Payment To complete your last...
28-8: Future Value of an Ordinary Annuity Problem 28-24 Required Lump-Sum Payment To complete your last year in business school and then go through law school, you will need $5,000 per year for 4 years, starting next year (that is, you will need to withdraw the first $5,000 one year from today). You...
1 answer
A survey was conducted that asked 1011 people how many books they had read in the...
A survey was conducted that asked 1011 people how many books they had read in the past year. Results indicated that x overbarx equals=13.6 books and s equals=16.6 books. Construct a 90​% confidence interval for the mean number of books people read. Interpret the interval. Construct a 90​...
1 answer
What things would you need to know to predict someone's success in a class as measure...
what things would you need to know to predict someone's success in a class as measure by scoring an A/B/C? What data would you need? How would you get the data? Are there things that can be done if you identify something to help correct? How can you 'catch' or 'identify' stud...
1 answer
4. Given that A and B are invertible matrices of the same size and that A(2X...
4. Given that A and B are invertible matrices of the same size and that A(2X + B)? A-1 = (AB-1A-1)-1, solve the equation for X(you have to simplify the answer)....
1 answer
Q3. (a) Define "Brittle Define "Brittle Materials and Ductile Materials in terms of Ductinty. (12 marks)...
Q3. (a) Define "Brittle Define "Brittle Materials and Ductile Materials in terms of Ductinty. (12 marks) (b) A solid metal bar of 30 mm tensile load of 120 kN. Find the stress, strain an Assume the Young's Modulus of the metal is 200 kN/mmº. Dar of 30 mm in diameter and 8 m in lengt...
1 answer
Heart A nne /content/file?cmd=view&content_id=_6580916_1&course_id=_48407_1 21) Ventricular repolarization Blackboard Help ? 22) Point after which the pressure...
Heart A nne /content/file?cmd=view&content_id=_6580916_1&course_id=_48407_1 21) Ventricular repolarization Blackboard Help ? 22) Point after which the pressure starts to more me 23) Point that represents the "dup" sound made by the heart. 24) Point after which the pressure starts to ...
1 answer
Practice: How many liters of a 0.200 M solution can be prepared from 1.50 grams of...
Practice: How many liters of a 0.200 M solution can be prepared from 1.50 grams of each of the solutes below? NaNO3 Lici CH,CH,OH...
1 answer
MS Company reported $670,000 Stockholder's Equity on January 1, 2017. Durisng the year, MS Company paid...
MS Company reported $670,000 Stockholder's Equity on January 1, 2017. Durisng the year, MS Company paid $35,000 in dividends. Expenses for 2017 were $560,000. At December 31, 2017, Stockholder's Equity was $900,000. What was the revenue for 2017? $825,000 $230,000 $995,000 $195,000...
1 answer
Please Explain how to get the answer if you can! Enter your answer in the provided...
Please Explain how to get the answer if you can! Enter your answer in the provided box. Acetylene burns in air according to the following equation: CH99)+0302 C029) + H206) AH = -1255.8 kJ I'xn Given sy of CO2(9)=–393.5 kJ/mol and sH of H2O(g)--241.8 kJ/mol, find all of C_H28). kJ/mol...
1 answer
Question 32 (4 points) Predict the product of this reaction: Hot H heat HO ОН ОН...
Question 32 (4 points) Predict the product of this reaction: Hot H heat HO ОН ОН 일 О он ОН...
1 answer
A company provided the following direct materials cost information. Compute the direct materials price variance. Standard...
A company provided the following direct materials cost information. Compute the direct materials price variance. Standard costs assigned: Direct materials standard cost (406,000 units @ $2.10/unit) $ 852,600 Actual costs: Direct Materials costs incurred (404,850 units @ $2...
1 answer
Question 3 (10 points) A1, C1, D1 Hassan company uses the proration approach to accounting for...
Question 3 (10 points) A1, C1, D1 Hassan company uses the proration approach to accounting for the overhead cost. At the end of 2019, the company records showed the following ending balances: COGS: $70,000; Finished goods: $20,000; Work in process: $10,000. The actual manufacturing overhead cost for...
1 answer
Compute the Jacobian for the transformation and. Bonus: Find the coordinates for the point in the...
Compute the Jacobian for the transformation and. Bonus: Find the coordinates for the point in the xy-Plane. 11. (7 pts.) Compute the Jacobian for the transformation x = ue' and y=ue". Bonus: Find the (u, v) coordinates for the point in the xy-Plane (3e, \e)....
1 answer
4. For the following reaction, tell how the equilibrium yield of iron(III) trichloride is affected by......
4. For the following reaction, tell how the equilibrium yield of iron(III) trichloride is affected by... 6C12(8) + 2Fe2O3(s) = 4FeCl3(s) + 302(8) (a) increasing Pa, (b) adding Fe20.. (c) selectively removing O2(e). (d) adding a catalyst. (e) removing part of the FeCl. (1) decreasing the total pressu...
1 answer
Or each scenario below, draw a graph of the market, property labeled, with the original equilbrium...
or each scenario below, draw a graph of the market, property labeled, with the original equilbrium marked as A. Show on the graph the proper shift and maurk the new equiläbrium as point B. State what happess to the equilibrium price and quantity Price A. Increase in demand Supply C. Increase in...
1 answer
Ochem 2 ch 22 720 Ch 22 Amines (100 pts) y 2320 aior product for each...
Ochem 2 ch 22 720 Ch 22 Amines (100 pts) y 2320 aior product for each of the following reactions or reaction sequences. Show chemistry where appropriate. Assume aqueous workup. stereochemist NaCN DMSO Nang DMSO H.NCHE HO H+ (cat.) HAN ~ CHO benzene -H,O H₂N P-TOH benzene -HO HN(CH3)2 p-TSOH be...