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();         }     } };

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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 nationReturn 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();
        }
    }
};

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Computational Systems
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education