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. program in C++
edit this code
| #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< | |
| } | |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
