SIT102 – Introduction to Programming Answers for 7.1P Arrays and Structs Student Name: Eiman Student ID: 5000834 Question 1: Briefly explain what each part of the following statement is doing:...

1 answer below »
Coding and 2 questions


SIT102 – Introduction to Programming Answers for 7.1P Arrays and Structs Student Name: Eiman Student ID: 5000834 Question 1: Briefly explain what each part of the following statement is doing: kingdom.knights.push_back( new_knight ); Question 2: What can we write in this program to determine the current number of knights in the kingdom? Write the code below: Include a screenshot of your program running here: Include a copy of your program code here:
Answered Same DaySep 25, 2021SIT102Deakin University

Answer To: SIT102 – Introduction to Programming Answers for 7.1P Arrays and Structs Student Name: Eiman Student...

Kushal answered on Sep 29 2021
160 Votes
1
A.
struct kingdom_data{
    string name;
    vector knight;
};
B.
void add_knight(kingdom_data &kingdom)
{
    string name;
    int age;
    getchar();
    knight_data new_knight;
    cout << "Enter name of Knight" << e
ndl;
    getline(cin,name);
    cout << "Enter age of Knight" << endl;
    cin >> age;
    new_knight.age=age;
    new_knight.name=name;
    kingdom.knight.push_back(new_knight);
}
C.
void write_kingdom(const kingdom_data &kingdom)
{
    cout << "##########################" << endl;
    cout << kingdom.name << endl;
    cout << "######### Knights ########" << endl;
    for(int i=0;i < kingdom.knight.size();i++)
    {
        write_knight(kingdom.knight[i]);
    }
    cout << "##########################" << endl;
}
D
void delete_knight(kingdom_data &kingdom, int index)
{
    if(index >=0 && index < kingdom.knight.size())
    {
        int last_idx;
        last_idx = kingdom.knight.size() - 1;
        for(int i=index;i        {
            kingdom.knight[i]=kingdom.knight[i+1];
        }
        kingdom.knight.pop_back();
    }
}
Q2
A
int select_knight(kingdom_data &kingdom)
{
    int index;
    for(int i=0;i    {
        cout << "Knight " << kingdom.knight[i].name << " has the following index " << i << endl;;
    }
    cout << "Write index: " << endl;
    cin >> index;
    if(index >=0 && index < kingdom.knight.size())
    {
        
        cout << "Knight " << kingdom.knight[index].name << "has been selected." << endl;
        cout << "Index: " << index << endl;
        
    }
    return index;
}
B
int option;
    do
    {
        int index,i=0;
    
        cout << " Main menu " << endl;
        cout << "Press 1 to Add Knight " << endl;
        cout << "Press 2 to Add Followers " << endl;
        cout << "Press 3 to Display Knights " << endl;
        cout << "Press 4 to Delete Knights " << endl;
        cout << "Press 5 to Exit " << endl;
        cin >> option;
        switch(option)
        {
            case 1:
                add_knight(my_kingdom);
                break;
            case 2:
                index=select_knight(my_kingdom);
                do
                {
                add_follower(my_kingdom.knight[index]);
                cout << "Add another Knight " << endl;
                cout << "Press 1 to Add followers to the same Knight" << endl;
                cout << "Press 2 to move on" << endl;
                cin >> i;
                }while(i!=2);
                break;
            case 3:
                write_kingdom(my_kingdom);
                break;
            case...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here