Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 2 Lnai 9285

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Joao Gama ,Alipio Jorge ,Carlos Soares

1st Edition

3319235249, 978-3319235240

More Books

Students also viewed these Databases questions