Question
Programming Assignment 3: Revise the C++ sequence Class to Use a Dynamic Array (Chapter 4) I NEED HELP PASSING A SCRIPTED TEST PROGRAM This is
Programming Assignment 3: Revise the C++ sequence Class to Use a Dynamic Array (Chapter 4)
I NEED HELP PASSING A SCRIPTED TEST PROGRAM
This is the code for the test that I keep failing:
int test3( ) { // In the next declarations, I am declaring a sequence called test. // Both before and after the sequence, I declare a small array of characters, // and I put the character 'x' into each spot of these arrays. // Later, if I notice that one of the x's has been changed, or if // I notice an 'x' inside of the sequence, then the most // likely reason was that one of the sequence's member functions accessed // the sequence's array outside of its legal indexes. char prefix[4] = {'x', 'x', 'x', 'x'}; sequence test; char suffix[4] = {'x', 'x', 'x', 'x'};
// Within this function, I create several different sequences using the // items in these arrays: double items1[1] = { 30 }; double items2[2] = { 10, 30 }; double items3[3] = { 10, 20, 30 }; size_t i; // for-loop control variable char *char_ptr; // Variable to loop at each character in a sequence's memory
// Build a sequence with three items 10, 20, 30, and remove the middle, // and last and then first. cout
// Build a sequence with three items 10, 20, 30, and remove the middle, // and then first and then last. cout
// Build a sequence with three items 10, 20, 30, and remove the first. cout
// Just for fun, fill up the sequence, and empty it! cout
// Make sure that the character 'x' didn't somehow get into the sequence, // as that would indicate that the sequence member functions are // copying data from before or after the sequence into the sequence. char_ptr = (char *) &test; for (i = 0; i
// Make sure that the prefix and suffix arrays still have four // x's each. Otherwise one of the sequence operations wrote outside of // the legal boundaries of its array. for (i = 0; i
// ************************************************************************** // int test4( ) // Performs some tests of resize. // Returns POINTS[4] if the tests are passed. Otherwise returns 0. // **************************************************************************
The parts that I fail^
Here is my code:
#include
sequence::sequence(size_type initial_capacity) {
data = new value_type[initial_capacity]; capacity = initial_capacity; used = 0; current_index = 0;
}
sequence::sequence(const sequence& source) {
capacity = source.capacity; data = new value_type[capacity]; used = source.used; current_index = source.current_index; for (int i = 0; i
}
sequence::~sequence() { delete[] data;
}
sequence::size_type sequence::size() const { return used;
}
bool sequence::is_item() const { return (current_index
}
sequence::value_type sequence::current() const {
assert(is_item()); return data[current_index];
}
void sequence::resize(size_type new_capacity) { if (new_capacity > used)
{
capacity = new_capacity; value_type* temp_data = new value_type[capacity];
for (int i = 0; i
}
}
void sequence::start() { current_index = 0;
}
void sequence::advance() {
assert(is_item()); current_index++;
}
void sequence::insert(const value_type& entry) { if (used >= capacity) resize(1 + used + used / 10);
if (!is_item()) current_index = 0;
for (size_type i = used; i > current_index; i--) data[i] = data[i - 1]; data[current_index] = entry; used++;
}
void sequence::attach(const value_type& entry) { if (used >= capacity) resize(1 + used + used / 10);
if (!is_item())
{ current_index = used; data[current_index] = entry; }
else
{
for (size_type i = used; i > (current_index + 1); i--) data[i] = data[i - 1]; data[current_index + 1] = entry; current_index++;
}
used++;
}
void sequence::remove_current() {
assert(is_item());
for (size_type i = current_index; i
if (!is_item()) current_index = used - 1;
}
void sequence::operator =(const sequence& source) {
if (this != &source) { capacity = source.capacity; delete[] data; data = new value_type[capacity]; used = source.used; current_index = source.current_index;
for (int i = 0; i
}
}
}
WHAT DO I NEED TO CHANGE TO PASS THIS TEST PROGRAM?
START OF TEST 3: Testing remove_current (4 points). Using attach to build a sequence with 10,30. Insert a 20 before the 30, so entire sequence is 10,20,30. Testing that size() returns 3 ... Passed. Testing that is_item() returns true ... Passed. The cursor should be at item [1] of the sequence (counting the first item as [@]). I will advance the cursor to the end of the sequence, checking that each item is correct... Passed. I'll call start() and look at the items one more time... The cursor should be at item [@] of the sequence (counting the first item as [@]). I will advance the cursor to the end of the sequence, checking that each item is correct... Passed. All tests passed for this sequence. Remove the 20, so entire sequence is now 10,30. Testing that size() returns 2 ... Passed. Testing that is item() returns true ... Passed. The cursor should be at item [1] of the sequence (counting the first item as [@]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. I'll call start() and look at the items one more time... The cursor should be at item [@] of the sequence (counting the first item as [@]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. All tests passed for this sequence. Remove the 30, so entire sequence is now just 10 with no cursor. Testing that size() returns 1 Passed. Testing that is_item() returns false Failed. Basic test of size() or is_item() failed. Test 3 failed. END OF TEST 3. START OF TEST 3: Testing remove_current (4 points). Using attach to build a sequence with 10,30. Insert a 20 before the 30, so entire sequence is 10,20,30. Testing that size() returns 3 ... Passed. Testing that is_item() returns true ... Passed. The cursor should be at item [1] of the sequence (counting the first item as [@]). I will advance the cursor to the end of the sequence, checking that each item is correct... Passed. I'll call start() and look at the items one more time... The cursor should be at item [@] of the sequence (counting the first item as [@]). I will advance the cursor to the end of the sequence, checking that each item is correct... Passed. All tests passed for this sequence. Remove the 20, so entire sequence is now 10,30. Testing that size() returns 2 ... Passed. Testing that is item() returns true ... Passed. The cursor should be at item [1] of the sequence (counting the first item as [@]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. I'll call start() and look at the items one more time... The cursor should be at item [@] of the sequence (counting the first item as [@]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. All tests passed for this sequence. Remove the 30, so entire sequence is now just 10 with no cursor. Testing that size() returns 1 Passed. Testing that is_item() returns false Failed. Basic test of size() or is_item() failed. Test 3 failed. END OF TEST 3Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started