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)

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

PROGRAM IN C++

Our BoardArray will store 5 entries maximum.

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:

(main.cpp in image)

entry.h

#include<iostream>
using namespace std;

struct entry {
    int score;
    const char* name = (char*) malloc( sizeof(char)*40 );
    const char* country = (char*) malloc( sizeof(char)*3 );

    bool compare(entry* other) {
        if (this->score > other->score) {
            return true;
        }
        return false;

        // return this->score > other->score;
    }

    void print() {
        cout << name << "(" << country << ") - " << score << endl; 
    }
};

board.h

#include "entry.h"
// similar to your list - an Abstract Data Type
class Board {
    virtual bool add(entry* e) = 0;
    virtual bool remove(const char* person) = 0;
    virtual entry* get(int pos) = 0;

    virtual bool ban_country(const char* nation) = 0;
    virtual int country_wins(const char* nation) = 0;
    virtual int exemplary(int score) = 0;
    virtual double average_score() = 0;

    virtual void print() = 0;
};

Implement the Functions in boardarray.h and Complete my code:

boardarray.h

#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) { // IMPROVE THIS CODE
        // 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) { //IMPROVE THIS CODE
        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) {

// CODE HERE
        return false;
    }

    int country_wins(const char* nation) {

// CODE HERE
        return 0;
    }

    virtual int exemplary(int score) {

// CODE HERE
        return 0;
    }

    virtual double average_score() {

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

 

nanno->country "THA";
47;
nanno->score
BoardArray b = new BoardArray();
int test;
cin >> test;
switch (test) {
case 1:
b->add (jayvince);
b->add (robin);
b->add (lily);
b->print();
break;
case 2:
b->add (jayvince);
b->add (robin);
b->add (marshall);
b->add (nanno);
b->add (ted);
cout << b->add (lily) << endl;
b->print();
break;
case 3:
b->add (jayvince);
b->add (robin);
b->add (marshall);
b->add (barney);
b->add (nanno);
b->add (ted);
cout << b->add (lily) << endl;
b->print();
break;
case 4:
b->add (lily);
b->add (robin);
b->add (marshall);
b->add (barney);
b->add (nanno);
b->add (ted);
cout <<h->add(iavvince) << endl.
Transcribed Image Text:nanno->country "THA"; 47; nanno->score BoardArray b = new BoardArray(); int test; cin >> test; switch (test) { case 1: b->add (jayvince); b->add (robin); b->add (lily); b->print(); break; case 2: b->add (jayvince); b->add (robin); b->add (marshall); b->add (nanno); b->add (ted); cout << b->add (lily) << endl; b->print(); break; case 3: b->add (jayvince); b->add (robin); b->add (marshall); b->add (barney); b->add (nanno); b->add (ted); cout << b->add (lily) << endl; b->print(); break; case 4: b->add (lily); b->add (robin); b->add (marshall); b->add (barney); b->add (nanno); b->add (ted); cout <<h->add(iavvince) << endl.
1 #include<iostream>
2 #include<string>
3 #include<iomanip>
4 #include "boardarray.h"
5 using namespace std;
6
8
9
3
1
2
3
4
5
5
B
2
1
2
1
3
4
5
7
B
9
A
int main() {
entry* jayvince (entry*) malloc(sizeof(entry) );
jayvince->name "Jay Vince Serato";
"PHI"
jayvince->country
jayvince->score 40;
entry* barney (entry*) malloc(sizeof (entry) );
barney->name "Barney Stinson";
barney->country "CAN";
barney->score
entry* robin
robin->name
robin->country
robin->score 60;
entry* ted
ted->name
45;
(entry*) malloc(sizeof (entry) );
"Robin Scherbatsky";
"CAN";
(entry*) malloc(sizeof(entry) );
"Ted Mosby";
ted->country "USA";
ted->score
55;
entry marshall (entry*) malloc(sizeof(entry) );
"Marshall Ericksen";
marshall >name
marshall >country "USA";
marshall score 36;
entry* lily
lily->name
lily->country
lily->score
(entry*) malloc(sizeof (entry) );
"Lily Aldrin";
"USA";
31;
entry nanno
(entry
nanno >name
"Nanno"
nanno->country "THA"
nanno >score 47;
malloc(sizeof (entry) );
BoardArray b new BoardArray();
int test;
cin >> test;
switch(test) {
Transcribed Image Text:1 #include<iostream> 2 #include<string> 3 #include<iomanip> 4 #include "boardarray.h" 5 using namespace std; 6 8 9 3 1 2 3 4 5 5 B 2 1 2 1 3 4 5 7 B 9 A int main() { entry* jayvince (entry*) malloc(sizeof(entry) ); jayvince->name "Jay Vince Serato"; "PHI" jayvince->country jayvince->score 40; entry* barney (entry*) malloc(sizeof (entry) ); barney->name "Barney Stinson"; barney->country "CAN"; barney->score entry* robin robin->name robin->country robin->score 60; entry* ted ted->name 45; (entry*) malloc(sizeof (entry) ); "Robin Scherbatsky"; "CAN"; (entry*) malloc(sizeof(entry) ); "Ted Mosby"; ted->country "USA"; ted->score 55; entry marshall (entry*) malloc(sizeof(entry) ); "Marshall Ericksen"; marshall >name marshall >country "USA"; marshall score 36; entry* lily lily->name lily->country lily->score (entry*) malloc(sizeof (entry) ); "Lily Aldrin"; "USA"; 31; entry nanno (entry nanno >name "Nanno" nanno->country "THA" nanno >score 47; malloc(sizeof (entry) ); BoardArray b new BoardArray(); int test; cin >> test; switch(test) {
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Concept of Threads
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