C++ Improve the following existing methods: bool add(entry* e) Improve the add method where you will return true if the entry has been successfully added (i.e. the entry is placed in the top 10), otherwise return false (i.e. if the entry did not make it to the top 10). bool remove(const char* person) Improve the remove method where you return true if an entry is successfully deleted (i.e. we have found an entry that matches the person), otherwise return false. Add the following additional methods: bool ban_country(const char* nation) Removes all the entries bearing the country equal to the given nation. Return true if at least one entry has been removed, otherwise return false. int country_wins(const char* nation) Returns the number of entries in the scoreboard who represents the given nation. int exemplary(int score) Returns how many entries in the scoreboard are greater than or equal to the given score. double average_score() Returns the average score of the entries in the scoreboard. If the scoreboard is empty, return 0. Given code: #include "board.h" #include #include #include using namespace std; class BoardArray : public Board { entry* array; int index; public: BoardArray() { // CONSTRUCTOR // Size changed to 5 array = (entry*) malloc ( sizeof(entry)*5 ); index = 0; } bool add(entry* e) { // Step 1: Find its rightful place int i; for (i = 0; i < index; i++) { // Get and compare the existing entry entry* existing = array + (i * sizeof(entry)); // 0 + 0 * 5 = 0 if (e->compare(existing)) { // return true if e is greater, rightful place break; } } // Step 2: Move the lesser entries to the right // WARNING! When index holds the max size (5), j will be 5-1 = 4 // Inside the for loop, you are accessing j+1 which in this case is 4+1=5, // which should not be accessible since it is supposedly ArrayIndexOutOfBounds. // You may want to modify the starting point of j as you see fit. for (int j = index-1; j >= i; j--) { // next (second) entry = current entry *(array + ( sizeof(entry) * (j+1) ) ) = *(array + ( sizeof(entry) * j ) ); } // Step 3: Add the entry *(array + ( sizeof(entry) * i ) ) = *e; index++; // Added temporarily. You may delete or move this. return false; } bool remove(const char* person) { for (int i = 0; i < index; i++) { entry* existing = array + (sizeof(entry) * i); if (!strcmp(existing->name, person)) { // MOVE all entries to the left starting to the next // We are to stop before we reach index-1 so as not to reach index-1+1 where data doesn't exist for (int j = i; j < index - 1; j++) { // current entry = next entry *(array + ( sizeof(entry) * (j) ) ) = *(array + ( sizeof(entry) * (j+1) ) ); } index--; break; } } // Added temporarily. You may delete or move this. return false; } entry* get(int pos) { return array + (sizeof(entry) * (pos-1) ); } bool ban_country(const char* nation) { return false; } int country_wins(const char* nation) { return 0; } virtual int exemplary(int score) { return 0; } virtual double average_score() { return 0; } void print() { for (int i = 0; i < index; i++) { entry* existing = array + (i * sizeof(entry)); // 0 + 0 * 5 = 0 cout << i << ": "; existing->print(); } } };
C++ Improve the following existing methods:
- bool add(entry* e)
Improve the add method where you will return true if the entry has been successfully added (i.e. the entry is placed in the top 10), otherwise return false (i.e. if the entry did not make it to the top 10).
- bool remove(const char* person)
Improve the remove method where you return true if an entry is successfully deleted (i.e. we have found an entry that matches the person), otherwise return false.
Add the following additional methods:
- bool ban_country(const char* nation)
Removes all the entries bearing the country equal to the given nation. Return true if at least one entry has been removed, otherwise return false.
- int country_wins(const char* nation)
Returns the number of entries in the scoreboard who represents the given nation.
- int exemplary(int score)
Returns how many entries in the scoreboard are greater than or equal to the given score.
- double average_score()
Returns the average score of the entries in the scoreboard. If the scoreboard is empty, return 0.
Given code:
#include "board.h"
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
class BoardArray : public Board {
entry* array;
int index;
public:
BoardArray() { // CONSTRUCTOR
// Size changed to 5
array = (entry*) malloc ( sizeof(entry)*5 );
index = 0;
}
bool add(entry* e) {
// Step 1: Find its rightful place
int i;
for (i = 0; i < index; i++) {
// Get and compare the existing entry
entry* existing = array + (i * sizeof(entry)); // 0 + 0 * 5 = 0
if (e->compare(existing)) { // return true if e is greater, rightful place
break;
}
}
// Step 2: Move the lesser entries to the right
// WARNING! When index holds the max size (5), j will be 5-1 = 4
// Inside the for loop, you are accessing j+1 which in this case is 4+1=5,
// which should not be accessible since it is supposedly ArrayIndexOutOfBounds.
// You may want to modify the starting point of j as you see fit.
for (int j = index-1; j >= i; j--) {
// next (second) entry = current entry
*(array + ( sizeof(entry) * (j+1) ) ) = *(array + ( sizeof(entry) * j ) );
}
// Step 3: Add the entry
*(array + ( sizeof(entry) * i ) ) = *e;
index++;
// Added temporarily. You may delete or move this.
return false;
}
bool remove(const char* person) {
for (int i = 0; i < index; i++) {
entry* existing = array + (sizeof(entry) * i);
if (!strcmp(existing->name, person)) {
// MOVE all entries to the left starting to the next
// We are to stop before we reach index-1 so as not to reach index-1+1 where data doesn't exist
for (int j = i; j < index - 1; j++) {
// current entry = next entry
*(array + ( sizeof(entry) * (j) ) ) = *(array + ( sizeof(entry) * (j+1) ) );
}
index--;
break;
}
}
// Added temporarily. You may delete or move this.
return false;
}
entry* get(int pos) {
return array + (sizeof(entry) * (pos-1) );
}
bool ban_country(const char* nation) {
return false;
}
int country_wins(const char* nation) {
return 0;
}
virtual int exemplary(int score) {
return 0;
}
virtual double average_score() {
return 0;
}
void print() {
for (int i = 0; i < index; i++) {
entry* existing = array + (i * sizeof(entry)); // 0 + 0 * 5 = 0
cout << i << ": ";
existing->print();
}
}
};
Step by step
Solved in 3 steps