Question

In: Computer Science

WRITE ONLY THE TO DO'S   in FINAL program IN C++ (necessary cpp and header files are...

WRITE ONLY THE TO DO'S   in FINAL program IN C++ (necessary cpp and header files are witten below)

"KINGSOM.h " program below:

#ifndef WESTEROS_kINGDOM_H_INCLUDED

#define WESTEROS_KINGDOM_H_INCLUDED

#include <iostream>

namespace westeros {

class Kingdom{

        public:

                char m_name[32];

                int m_population; };

        void display(Kingdom&);                      }

#endif }

Kingdom.cpp Program below:

#include <iostream>

#include "kingdom.h"

using namespace std;

namespace westeros {

        void display(Kingdom& pKingdom) {

                cout << pKingdom.m_name << ", population " << pKingdom.m_population << endl;

                                                             

FINAL:

#include<iostream>

#include"kingdom.h"

usingnamespace std;

usingnamespace westeros;

int main(void)

{

int count = 0; // the number of kingdoms in the array

// TODO: declare the pKingdoms pointer here (don't forget to initialize it)

    cout <<"=========="<< endl

<<"Input data"<< endl

<<"=========="<< endl

<<"Enter the number of kingdoms: ";

    cin >> count;

    cin.ignore();

// TODO: allocate dynamic memory here for the pKingdoms pointer

for (int i = 0; i < count; ++i)

    {

// TODO: add code to accept user input for the pKingdoms array

    }

    cout <<"=========="<< endl << endl;

// testing that "display(...)" works

    cout <<"------------------------------"<< endl

<<"The first kingdom of Westeros"<< endl

<<"------------------------------"<< endl;

    display(pKingdoms[0]);

    cout <<"------------------------------"<< endl << endl;

// testing that the first overload of "display(...)" works

    display(pKingdoms, count);

    cout << endl;

// testing that the second overload of "display(...)" works

    display(pKingdoms, count, 345678);

    cout << endl;

// testing that the third overload of "display(...)" works

    display(pKingdoms, count, "Mordor");

    cout << endl;

    display(pKingdoms, count, "The_Vale");

    cout << endl;

// TODO: deallocate the dynamic memory here

return0;

}

OUTPUT SAMPLE:

Output Sample:

==========

Input data

==========

Enter the number of kingdoms: 5

Enter the name for kingdom #1: The_Riverlands

Enter the number people living in The_Riverlands: 123456

Enter the name for kingdom #2: The_Vale

Enter the number people living in The_Vale: 234567

Enter the name for kingdom #3: The_Westernlands

Enter the number people living in The_Westernlands: 345678

Enter the name for kingdom #4: The_Stormlands

Enter the number people living in The_Stormlands: 456789

Enter the name for kingdom #5: The_Reach

Enter the number people living in The_Reach: 567890

==========

------------------------------

The first kingdom of Westeros

------------------------------

The_Riverlands, population 123456

------------------------------

------------------------------

Kingdoms of Westeros

------------------------------

1. The_Riverlands, population 123456

2. The_Vale, population 234567

3. The_Westernlands, population 345678

4. The_Stormlands, population 456789

5. The_Reach, population 567890

------------------------------

Total population of Westeros: 1728380

------------------------------

------------------------------

Kingdoms of Westeros with more than 345678 people

------------------------------

The_Westernlands, population 345678

The_Stormlands, population 456789

The_Reach, population 567890

------------------------------

------------------------------

Searching for kingdom Mordor in Westeros

------------------------------

Mordor is not part of Westeros.

------------------------------

------------------------------

Searching for kingdom The_Vale in Westeros

------------------------------

The_Vale, population 234567

------------------------------

Solutions

Expert Solution

//main.cpp


#include <iostream>
#include "kingdom.h"

using namespace std;
using namespace westeros;

int main(void) {
int count = 0; // the number of kingdoms in the array

// TODO: declare the pKingdoms pointer here (don't forget to initialize it)
Kingdom *pKingdoms;

cout << "==========" << endl
<< "Input data" << endl
<< "==========" << endl
<< "Enter the number of kingdoms: ";
cin >> count;
cin.ignore();

// TODO: allocate dynamic memory here for the pKingdoms pointer
pKingdoms = new Kingdom[count];

for (int i = 0; i < count; ++i) {
// TODO: add code to accept user input for the kingdoms array

cout << "Enter the name for kingdom #" << i + 1 << ": ";
cin >> pKingdoms[i].m_name;
cout << "Enter the number people living in " << pKingdoms[i].m_name
<< ": ";
cin >> pKingdoms[i].m_population;
}
cout << "==========" << endl << endl;

// testing that "display(...)" works
cout << "------------------------------" << endl
<< "The first kingdom of Westeros" << endl
<< "------------------------------" << endl;
display(pKingdoms[0]);
cout << "------------------------------" << endl << endl;

// testing that the first overload of "display(...)" works
display(pKingdoms, count);
cout << endl;

// testing that the second overload of "display(...)" works
display(pKingdoms, count, 345678);
cout << endl;

// testing that the third overload of "display(...)" works
display(pKingdoms, count, "Mordor");
cout << endl << endl;

display(pKingdoms, count, "The_Vale");
cout << endl;

// TODO: deallocate the dynamic memory here
delete[] pKingdoms;

return 0;
}

========================================================================

//kingdom.cpp


// TODO: include the necessary headers
#include "kingdom.h"
#include <cstring>
#include <iostream>

using namespace std;
// TODO: the westeros namespace
namespace westeros {
// TODO:definition for display(...)
void display(Kingdom &kingdom) {
cout << kingdom.m_name << ", population " << kingdom.m_population << endl;
}

void display(Kingdom kingdom[], int num) {
int TOTAL_POPULATION = 0;
cout << "------------------------------" << endl;
cout << "Kingdoms of Westeros" << endl;
cout << "------------------------------" << endl;
for (int i = 0; i < num; i++) {
cout << i + 1 << ". " << kingdom[i].m_name << ", population "
<< kingdom[i].m_population << endl;
TOTAL_POPULATION += kingdom[i].m_population;
}
cout << "------------------------------" << endl;
cout << "Total population of Westeros: " << TOTAL_POPULATION << endl;
cout << "------------------------------" << endl;
}

void display(Kingdom kingdom[], int num, int MIN_POPULATION) {
cout << "------------------------------" << endl;
cout << "Kingdoms of Westeros with more than " << MIN_POPULATION
<< " people" << endl;
cout << "------------------------------" << endl;
for (int i = 0; i < num; i++) {
if (kingdom[i].m_population >= MIN_POPULATION) {
cout << kingdom[i].m_name << ", population "
<< kingdom[i].m_population << endl;
}
}
cout << "------------------------------" << endl;
}

void display(Kingdom kingdom[], int num, const char *KINGDOM_NAME) {
bool kingdom_found = false;
cout << "------------------------------" << endl;
cout << "Searching for kingdom " << KINGDOM_NAME << " in Westeros" << endl;
cout << "------------------------------" << endl;
for (int i = 0; i < num; i++) {
if (!strcmp(kingdom[i].m_name, KINGDOM_NAME)) {
cout << kingdom[i].m_name << ", population "
<< kingdom[i].m_population << endl;
kingdom_found = true;
}
}
if (!kingdom_found) {
cout << KINGDOM_NAME << " is not part of Westeros." << endl;
}
cout << "------------------------------" << endl;
}
}

==================================================================

//kingdom.h

// TODO: header safeguards
#ifndef KINGDOM_H
#define KINGDOM_H

// TODO: westeros namespace
namespace westeros {

// TODO: define the class Kingdom in the westeros namespace
class Kingdom {
public:
char m_name[32];
int m_population;
};


// TODO: add the declaration for the function display(...),
// also in the westeros namespace
void display(Kingdom&);
void display(Kingdom[], int);
void display(Kingdom[], int, int);
void display(Kingdom[], int, const char*);

}


#endif

====================================================================

sample output:


Related Solutions

Write an entire program (with a main function and necessary header files included) that declares an...
Write an entire program (with a main function and necessary header files included) that declares an 80 byte character buffer, prompts a user to enter a single word (no spaces) from the default input device (e.g. the keyboard), which is stored in the buffer, and then reverses the string while overwriting the buffer. Print the sting before and after the reversal. The program transcript should resemble the following output: $ ./program.exe enter string: nvidia before: nvidia after: aidivn in C
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course,...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course, and a simple program to test it, according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array...
Write the header and the implementation files (.h and .cpp separately) for a class called Course,...
Write the header and the implementation files (.h and .cpp separately) for a class called Course, and a simple program to test it (C++), according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only useiostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Create : locomotive.h, locomotive.cpp, main.cpp Locomotive Class Hierarchy Presented here will be a class diagram depicting the nature of the class hierarchy formed between a parent locomotive class and its children, the two kinds of specic trains operated. The relationship is a...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only use iostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Given : locomotive.h, locomotive.cpp, main.cpp -> https://www.chegg.com/homework-help/questions-and-answers/complete-following-task-c--separate-class-header-cpp-files-useiostream-string-sstream-crea-q39733428 Create : DieselElectric.cpp DieselElectric.h DieselElectric dieselElectric -fuelSupply:int --------------------------- +getSupply():int +setSupply(s:int):void +dieselElectric(f:int) +~dieselElectric() +generateID():string +calculateRange():double The class variables are as follows: fuelSuppply: The fuel supply of the train in terms of a number of kilolitres...
C++ question. Need all cpp and header files. Part 1 - Polymorphism problem 3-1 You are...
C++ question. Need all cpp and header files. Part 1 - Polymorphism problem 3-1 You are going to build a C++ program which runs a single game of Rock, Paper, Scissors. Two players (a human player and a computer player) will compete and individually choose Rock, Paper, or Scissors. They will then simultaneously declare their choices and the winner is determined by comparing the players’ choices. Rock beats Scissors. Scissors beats Paper. Paper beats Rock. The learning objectives of this...
i need an example of a program that uses muliple .h and .cpp files in c++...
i need an example of a program that uses muliple .h and .cpp files in c++ if possible.
Write C++ program (submit the .cpp,.h, .sln and .vcxproj files) Problem 1. Generate 100 random numbers...
Write C++ program (submit the .cpp,.h, .sln and .vcxproj files) Problem 1. Generate 100 random numbers of the values 1-20 in an input.txt. Now create a binary search tree using the numbers of the sequence. The tree set should not have any nodes with same values and all repeated numbers of the random sequence must be stored in the node as a counter variable. For example, if there are five 20s’ in the random sequence then the tree node having...
Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork...
Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork class. The features of an ArtWork are: Artist name (e.g. Vincent vanGogh or Stan Getz) Medium (e.g. oil painting or music composition) Name of piece (e.g. Starry Night or Late night blues) Year (e.g. 1837 or 1958)
C++ Goals: Write a program that works with binary files. Write a program that writes and...
C++ Goals: Write a program that works with binary files. Write a program that writes and reads arrays to and from binary files. Gain further experience with functions. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of file, a pointer to an int array, and the size of the array. The function should open the specified file in binary made, write the contents into the array, and then close the file. write another...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT