Question: Write a program in C++ with the following requirements: Give C++ code for performing add(e) and remove(i) functions for game entries stored in an array

Write a program in C++ with the following requirements: Give C++ code for performing add(e) and remove(i) functions for game entries stored in an array a, as in class Scores in Section 3.1.1, except this time, dont maintain the game entries in order. Assume that we still need to keep n entries stored in indices 0 to n 1. Implement the add and remove functions without using any loops, so that the number of steps they perform does not depend on n.

score.cpp

#include "Scores.h"

Scores::Scores(int maxEnt) { // constructor

maxEntries = maxEnt; // save the max size

entries = new GameEntry[maxEntries]; // allocate array storage

numEntries = 0; // initially no elements

}

Scores::~Scores() { // destructor

delete[ ] entries;

}

void Scores::add(const GameEntry& e) { // add a game entry

int newScore = e.getScore(); // score to add

if (numEntries == maxEntries) { // the array is full

if (newScore <= entries[maxEntries-1].getScore())

return; // not high enough - ignore

}

else numEntries++; // if not full, one more entry

int i = numEntries - 2; // start with the next to last

while ( i >= 0 && newScore > entries[i].getScore() ) {

entries[i+1] = entries[i]; // shift right if smaller

i--;

}

entries[i+1] = e; // put e in the empty spot

}

GameEntry Scores::remove(int i) throw(IndexOutOfBounds) {

if ((i < 0) || (i >= numEntries)) // invalid index

throw IndexOutOfBounds("Invalid index");

GameEntry e = entries[i]; // save the removed object

for (int j = i+1; j < numEntries; j++)

entries[j-1] = entries[j]; // shift entries left

numEntries--; // one fewer entry

return e; // return the removed object

}

void Scores::printAll(){

cout<

for (int i = 0; i < numEntries; i++)

cout<

cout<

}

void Scores::printIndex(int indx)throw(IndexOutOfBounds){

if ((indx < 0) || (indx >= numEntries)) // invalid index

throw IndexOutOfBounds("Invalid index");

cout<

cout<

cout<

}

//////////////////////////////////////////////////

GameEntries

#include "GameEntry.h"

GameEntry::GameEntry(const string& n, int s) : name(n), score(s) {

}

GameEntry::~GameEntry() {

// TODO Auto-generated destructor stub

}

string GameEntry::getName() const {

return name;

}

int GameEntry::getScore() const {

return score;

}

////////////////////////////////////////////

main

#include "Scores.h"

int main() {

Scores scores(4);

GameEntry ge1("test0", 5);

scores.printAll();

scores.add(ge1);

// scores.printAll();

//

// GameEntry ge2("mira", 10);

//

//

//

// GameEntry ge3("test1", 3);

//

// GameEntry ge4("test2", 5);

// scores.add(ge1);

// scores.add(ge2);

// scores.printIndex(1);

// scores.add(ge3);

// scores.add(ge4);

//

// scores.printAll();

//

// scores.remove(1);

// scores.remove(1);

// scores.printAll();

//// scores.remove(1);

//// scores.printAll();

//// scores.remove(1);

// scores.remove(1);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!