Write a C++ code to implement the sequence class to store double. The implementa
ID: 3883752 • Letter: W
Question
Write a C++ code to implement the sequence class to store double. The implementation will include a header file, an implementation file, an application file. The sequence class will include the following operartions creates a sequence with no element, int size():return the total number of elements, void start () : set context to the first element of the sequence, double current() : retrieve the current element of the sequence, void advance() : set context to the next element of the sequence, bool is_item () : return true if current context is a valid element, false otherwise, void insert(double &) : adds a new element before the current void attach(double &) :adds a new element after the current, removes the current element. Each line should include an explaination. In the application file will have the menu list and ask users to choose which operations that they want.
Explanation / Answer
PROGRAM CODE:
Sequence.h
#ifndef SEQUENCE_H
#define SEQUENCE_H
class Sequence()
{
private:
double *data;
int dataSize;
int currentIndex;
public:
Sequence();
void start();
int size();
double current();
void advance();
bool is_item ();
void insert(double &);
void attach(double &);
};
#endif SEQUENCE_H
Sequence.cpp
#include "Sequence.h"
Sequence::Sequence()
{
dataSize = 0;
currentIndex = -1;
data = NULL;
}
void Sequence::start()
{
if(dataSize>0)
{
currentIndex = 0;
}
}
int Sequence::size()
{
return dataSize;
}
double Sequence::current()
{
return data[currentIndex];
}
void Sequence::advance()
{
currentIndex++;
}
bool Sequence::is_item()
{
if(currentIndex < dataSize)
return true;
else return false;
}
void Sequence::insert(double &myData)
{
if(dataSize == 0)
{
data = new double[10];
data[0] = myData;
}
else
{
double prevData = data[currentIndex-1];
data[currentIndex-1] = myData;
for (int i = currentIndex; i < dataSize-1; i++)
{
data[i] = prevData;
prevData = data[i+1];
}
data[dataSize++] = prevData;
}
}
void Sequence::attach(double &myData)
{
if(dataSize == 0)
{
data = new double[10];
data[0] = myData;
}
else
{
double nextData = data[currentIndex+1];
data[currentIndex+1] = myData;
for (int i = currentIndex+2; i < dataSize-1; i++)
{
data[i] = nextData;
prevData = data[i+1];
}
data[dataSize++] = nextData;
}
}
Main.cpp
#include "Sequence.h"
int main() {
Sequence seq;
seq.insert(54.9);
seq.insert(32.9);
seq.start();
seq.advance();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.