(C++) Change the following program so that it uses a dynamic array instead of a static one. Test
it with sequence_exam2.cpp.
sequence1.cpp
#include \"sequence1.h\"
#include
#include
namespace sequencelab
{
sequence::sequence()
{
used = 0;
current_index = 0;
}
void sequence::start()
{
if (used > 0)
{
current_index = 0;
}
else
{
current_index = used;
}
}
void sequence::advance()
{
if (is_item())
{
current_index++;
}
else
{
std::cout << \"Nothing here!\" << std::endl;
}
}
void sequence::insert(const value_type& figure)
{
if (size() < CAPACITY)
{
if (!is_item())
{
start();
}
for (size_t i = used; i > current_index; i--)
{
data[i] = data[i-1];
}
data[current_index] = figure;
used++;
}
else
{
std::cout << \"Did not insert, size is larger/equal to the capacity.\" << std::endl;
}
}
void sequence::attach(const value_type& figure)
{
if (size() < CAPACITY)
{
if (is_item())
{
for (size_t i = used; i > current_index + 1; i--)
{
data[i] = data[i-1];
}
data[current_index + 1] = figure;
current_index++;
used++;
}
else
{
data[used] = figure;
current_index = used;
used++;
}
}
else
{
std::cout << \"Did not attach, size is larger/equal to the capacity.\" << std::endl;
}
}
void sequence::remove_current()
{
if (is_item())
{
for (size_t i = current_index; i < used; i++)
{
data[i] = data[i+1];
}
used--;
}
else
{
std::cout << \"Nothing here!\" << std::endl;
}
}
sequence::size_type sequence::size() const
{
return used;
}
bool sequence::is_item() const
{
return ((current_index >= 0) && (current_index < used) && (used != 0));
}
sequence::value_type sequence::current() const
{
if (is_item())
{
return data[current_index];
}
else
{
std::cout << \"Nothing here!\" << std::endl;
}
}
}
----------------------------------------------------------------------------------
sequence1.h
#ifndef MAIN_SAVITCH_SEQUENCE_H
#define MAIN_SAVITCH_SEQUENCE_H
#include
namespace sequencelab
{
class sequence
{
public:
typedef double value_type;
typedef std::size_t size_type;
static const size_type CAPACITY = 30;
sequence( );
void insert(const value_type& figure);
void attach(const value_type& figure);
void start( );
void advance( );
void remove_current( );
value_type current( ) const;
size_type size( ) const;
bool is_item( ) const;
private:
value_type data[CAPACITY];
size_type used;
size_type current_index;
};
}
#endif
------------------------------------------------------------------------------------
sequence_exam2.cpp
#include // Provides cout.
#include // Provides memcpy.
#include // Provides size_t.
#include \"sequence2.h\" // Provides the Sequence class with double items.
using namespace std;
using namespace main_savitch_4;
// Descriptions and points for each of the tests:
const size_t MANY_TESTS = 7;
const int POINTS[MANY_TESTS+1] = {
21, // Total points for all tests.
4, // Test 1 points
4, // Test 2 points
4, // Test 3 points
2, // Test 4 points
2, // Test 5 points
2, // Test 6 points
3 // Test 7 points
};
const char DESCRIPTION[MANY_TESTS+1][256] = {
\"tests for sequence class with a dy.