write a c++ program which will give following output: Output Sample: ==========
ID: 3784154 • Letter: W
Question
write a c++ program which will give following output:
Output Sample:
==========
Input data
==========
Enter the number of kingdoms: 2
Enter the name for kingdom #1: The_Vale
Enter the number people living in The_Vale: 234567
Enter the name for kingdom #2: The_Reach
Enter the number people living in The_Reach: 567890
==========
------------------------------
The first kingdom of Westeros
------------------------------
The_Vale, population 234567
------------------------------
here is the half completed code below,write the to do part which is half done
#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 to null)
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;
// TODO: deallocate the dynamic memory here
return 0;
}
Explanation / Answer
#include <iostream>
//#include "kingdom.h"
using namespace std;
//using namespace westeros;
void display(char s[])
{
cout<<s;
}
int main(void)
{
int count = 0; // the number of kingdoms in the array
char **pKingdoms=NULL;
int size=1000;
cout<< "==========" << endl
<< "Input data" << endl
<< "==========" << endl
<< "Enter the number of kingdoms: ";
cin >> count;
cin.ignore();
//dynamic allocation
pKingdoms = new char *[count];
for( int i = 0 ; i < count ; i++ )
pKingdoms[i] = new char[100];
int people[count];
for (int i = 0; i < count; ++i)
{
cout<<" Enter the name for kingdom #"<<i+1<<": ";
cin>>pKingdoms[i];
cout<<" Enter number of people living in "<<pKingdoms[i]<<" ";
cin>>people[i];
}
cout << "==========" << endl << endl;
// testing that "display(...)" works
cout << "------------------------------" << endl
<< "The first kingdom of Westeros" << endl
<< "------------------------------" << endl;
display(pKingdoms[0]);
cout << " ------------------------------" << endl << endl;
//deallocation
for( int i = 0 ; i < count ; i++ )
delete [] pKingdoms[i];
delete [] pKingdoms;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.