1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
using namespace std;
class Country
{
private:
char name[30];
int population;
int area;
public:
Country();
Country(char* name, int population, int area);
~Country(){};
char* getName();
int getPopulation();
int getArea();
void setName(char name[]);
void setPopulation(int population);
void setArea(int area);
void display();
double getDensity();
static void display_list(int SIZE, Country *countries)
{
for(int i = 0; i < SIZE; i++)
countries[i].display();
}
};
Country::Country(){}
Country::Country(char n[], int population, int area)
{
strcpy(name, n);
this->population = population;
this->area = area;
}
char* Country::getName(){return name;}
int Country::getPopulation(){return population; }
int Country::getArea(){ return area;}
void Country::setName(char n[]){strcpy(name, n);}
void Country::setPopulation(int population){this->population = population;}
void Country::setArea(int area){this->area = area;}
double Country::getDensity(){return population/area;}
void Country::display()
{
cout
<< setw(15) << left << name
<< setw(15) << left << population
<< setw(10) << left << area
<< setw(10) << left << getDensity()
<< '\n';
}
void sortByPopulation(Country counrties[], int SIZE);
void sortByPopulationDensity(Country counrties[], int SIZE);
void sortByName(Country counrties[], int SIZE);
int main()
{
const int SIZE = 7;
Country countries[SIZE];
countries[0] = Country{"England" , 55980000, 275};
countries[1] = Country{"Scotland", 5454000, 65};
countries[2] = Country{"Ireland" , 4904000, 186};
countries[3] = Country{"Portugal", 10280000, 289};
countries[4] = Country{"Panama" , 11460000, 288};
countries[5] = Country{"Angola" , 20000000, 991};
countries[6] = Country{"Australia" , 20000000, 992};
cout << "UNSORTED\n";
Country::display_list(SIZE, countries);
cout << "\n\n";
sortByPopulation(countries, SIZE);
cout << "SORTED BY POPULATION (ASC)\n";
Country::display_list(SIZE, countries);
cout << "\n\n";
sortByPopulationDensity(countries, SIZE);
cout << "--SORTED BY POPULATION DENSITY (ASC)\n";
Country::display_list(SIZE, countries);
cout << "\n\n";
sortByName(countries, SIZE);
cout << "SORTED BY NAME (ASC)\n";
Country::display_list(SIZE, countries);
cout << "\n\n";
sortByName(countries, SIZE);
sortByPopulationDensity(countries, SIZE);
cout << "SORTED BY NAME THEN DENSITY (ASC)\n";
Country::display_list(SIZE, countries);
cout << "\n\n";
sortByPopulationDensity(countries, SIZE);
sortByName(countries, SIZE);
cout << "SORTED BY DENSITY THEN NAME (ASC)\n";
Country::display_list(SIZE, countries);
cout << "\n\n";
return 0;
}
void sortByPopulation(Country array[], int SIZE)
{
// This Logic will Sort the Array of elements in Ascending order
Country temp;
for (int i = 0; i < SIZE; i++)
{
for (int j = i + 1; j < SIZE; j++)
{
if (array[i].getPopulation() > array[j].getPopulation())
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
void sortByPopulationDensity(Country array[], int SIZE)
{
// This Logic will Sort the Array of elements in Ascending order
Country temp;
for (int i = 0; i < SIZE; i++)
{
for (int j = i + 1; j < SIZE; j++)
{
if ( array[i].getDensity() > array[j].getDensity() ) // <--
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
void sortByName(Country array[], int SIZE)
{
// This Logic will Sort the Array of elements in Ascending order
Country temp;
for (int i = 0; i < SIZE; i++)
{
for (int j = i + 1; j < SIZE; j++)
{
if (strcmp(array[i].getName(), array[j].getName()) > 0)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
|