Question: Hello, I need help creating these two functions. void removeFighter(Competition& mycompetition,const Fighter& loserFighter): allows the program to remove a fighter from a competition. This function
Hello, I need help creating these two functions.
void removeFighter(Competition& mycompetition,const Fighter& loserFighter): allows the program to remove a fighter from a competition. This function implements the following pseudo code:
- Dynamically allocate a new Array of type Fighter in a Fighter pointer. (The size of the new Fighter Array should be one less than the size of the Competition fighters so the loser fighter can be removed.)
- Find the index of the loserFighter using the function findFighter.
- If the loserFighter is found in the Competition, in a loop with two indices (One for the new fighter array and one for the Competition fighter array), copy the data from the competition fighters array (provided as the function parameter) to the new array of fighters except for the loserFighter. This will remove the loser (Make sure the index of the new fighter array is increased in each repetition only if the current fighter index is not equal to the found index of the loserFighter in the Competition).
- reduce the number of fighters in the Competition by one
- Remove the fighters array of the Competition from memory (delete the fighters array in Competition object)
- Set the fighters pointer in the Competition to the new fighters pointer.
- This completes removing a fighter and the function ends
int findFighter(const Competition& mycompetition,const Fighter& theFighter): returns the index of a desired fighter. It uses theFighter name as search criteria. If the fighter is not found, this function returns -1.
- Define an integer to keep the found index and initially set it to -1
- Start by looping through the fighters in the Competition.
- Compare each fighter's name in the Competition with theFighter argument's name. If there is a match keep the found index and end the loop;
- Return the found index (it will be -1 if no match were found)
Here is project information/blank files:https://github.com/Seneca-244200/OOP-Workshops/tree/main/WS02
//Fighter.cpp #define _CRT_SECURE_NO_WARNINGS #include "Fighter.h" #includenamespace sdds { void fighter(char* name) { std::cout << "Enter Fighter Name: "; std::cin >> name; } void fighter(int& power) { std::cout << "Enter Fighter Power: "; std::cin >> power; } void display(const Fighter& myFighter) { std::cout << "Name: " << myFighter.name << ", Power: " << myFighter.power << std::endl; } void display(const Competition& mycompetition) { for (int i = 0; i < mycompetition.numfighters; i++) { display(mycompetition.fighters[i]); } } void removeFighter(Competition& mycompetition, const Fighter& loserFighter) { } int findFighter(const Competition& mycompetition, const Fighter& theFighter) { } } // namespace sdds
Fighter.h
#ifndef SDDS_FIGHTER_H #define SDDS_FIGHTER_H namespace sdds { const int MAX_NAME = 50; struct Fighter { char name[MAX_NAME+1]; int power; }; struct Competition { Fighter* fighters; int numfighters; }; void fighter(char* name); void fighter(int& power); void display(const Fighter& myFighter); void display(const Competition& mycompetition); void addFighter(Competition& mycompetition); void fight(Competition& mycompetition, Fighter& f1, Fighter& f2); bool fight(const Fighter& f1, const Fighter& f2); void removeFighter(Competition& mycompetition, const Fighter& loserFighter); int findFighter(const Competition& mycompetition, const Fighter& theFighter); } #endif /* FIGHTER_H */
mainKombat.cpp
#include#include "Fighter.h" using namespace std; using namespace sdds; int main() { Competition mycompetition = {nullptr, 0}; cout << "*********************************" << endl; cout << "Welcome to OOP244-Mortal Kombat" << endl; cout << "*********************************" << endl; int i; for (i = 0; i < 4; i++) { addFighter(mycompetition); } cout << "*********************************" << endl; cout << "List of fighters:" << endl; display(mycompetition); cout << "*********************************" << endl; cout << "Let the fights begin:" << endl; cout << "*********************************" << endl; while (mycompetition.numfighters > 1) { fight(mycompetition, mycompetition.fighters[0], mycompetition.fighters[1]); if (mycompetition.numfighters > 1) { cout << "*********************************" << endl; cout << "List of fighters Still Standing:" << endl; display(mycompetition); cout << "*********************************" << endl; } } cout << "*********************************" << endl; cout << "The winner of the competition is:" << endl; display(mycompetition); delete[] mycompetition.fighters; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
