Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement C++ sequence container class that utilizes a dynamic array. Copy and paste the invariant from sequence.h to your sequence.cpp file. Update the invariant for

Implement C++ sequence container class that utilizes a dynamic array.

  1. Copy and paste the invariant from sequence.h to your sequence.cpp file. Update the invariant for your implementation.
  2. Do your work in small pieces. For example, my first version of the sequence had only a constructor, to_first, insert, next, and current. My other member functions started out as stubs.
  3. When a member functions needs to increase the size of the dynamic array, it is a good idea to increase that size by at least 10% (rather than by just one item).
  4. Do not modify the sequence header file
  5. The header has already been completed as well as a simple driver.

Provided files:

sequence.h: The header and prototype of the sequence class. You will be implementing all of the functions that are not already defined inline. All of your implementation must go in sequence.cpp and not modify this header.

main.cpp: Driver of the sequence. A rough start to creating an awesome test driver for your code. It would be wise to build this first.

Hints

Implement and Test Small Pieces

Dont tackle to whole project at once. Start by implementing what you can, using stubs for the harder functions. A stub is the implementation of a function with the lines of the body omitted. For example:

bool sequence::is_item( ) const

{

return true; // TODO: This is just a stub, to be implemented later.

}

Other Requirements

  • ALL member and non-member functions defined in sequence.h must be fully implemented, taking into account what the pre/post conditions say. You do not need to confirm any preconditions.
  • Your container must not produce any output, NO couts
  • Your code must not add any global variables
  • For full credit your code should have zero compiler warnings.
  • Your code will be checked for memory leaks. Memory leaks will be verified via a tool called valgrind as well as visually.
  • The data structure used for sequence must be a dynamic array
  • Usage of any STL containers will affect grade detrimentally.
  • Usage of recursion will result in no credit
  • You may not modify the provided header file.
  • Avoid all STL includes in sequence.cpp, however, cassert is allowed if you want to verify preconditions.
  • Only submit the file(s) listed in deliverables, submitting object files, IDE project files, and executables will impact grade.
  • Dont forget to document your code, see course outline for details.
  • #ifndef PROJECT_SEQUENCE_H
  • #define PROJECT_SEQUENCE_H
  • #include // Provides size_t
  • namespace DS
  • {
  • class sequence
  • {
  • public:
  • // TYPEDEFS and MEMBER CONSTANTS
  • typedef double value_type;
  • typedef std::size_t size_type;
  • static const size_type DEFAULT_CAPACITY = 30;
  • // CONSTRUCTORS and DESTRUCTOR
  • sequence(size_type initial_capacity = DEFAULT_CAPACITY);
  • sequence(const sequence& source);
  • ~sequence( );
  • // MODIFICATION MEMBER FUNCTIONS
  • void resize(size_type new_capacity);
  • void to_first();
  • void to_last();
  • void prev( );
  • void next( );
  • void insert(const value_type& entry);
  • void append(const value_type& entry);
  • void remove( );
  • sequence& operator=(const sequence& source);
  • // CONSTANT MEMBER FUNCTIONS
  • size_type size( ) const { return used; };
  • bool is_item( ) const;
  • value_type current_value( ) const;
  • value_type current_index( ) const { return current; };
  • value_type standard_deviation() const;
  • private:
  • value_type* data;
  • size_type used;
  • size_type current;
  • size_type capacity;
  • };
  • }
  • #endif //PROJECT_SEQUENCE_H

#include

#include

#include "sequence.h"

//Precondition: none

using namespace DS;

int main() {

//Disclaimer, this is ONLY a test to check for valid compilation

// It does NOT check the validness of your implementation

// TODO: Student creates valid test cases to check for all edge cases

sequence s1;

append_to_sequence(s1, {2, 4, 8, 16, 32, 64});

s1.to_first(); //Reset position to first item

std::cout << s1 << std::endl;

std::cout >> s1 << std::endl;

std::cout << s1 << std::endl;

sequence s2;

append_to_sequence(s2, {600, 470, 170, 430, 300});

s2.to_first();

std::cout << s2 << std::endl;

std::cout << s2.standard_deviation() << std::endl;

s1.to_first();

s1.next();

s1.insert(30);

s1.to_first();

s1.remove();

std::cout << s1 << std::endl;

s1.resize(60);

std::cout << s1 << std::endl;

return 0;

}

void append_to_sequence(DS::sequence& seq, const std::initializer_list list) {

for ( auto item : list )

seq.append(item);

}

void move_position_to(DS::sequence& seq, size_t new_index/* = 0*/) {

//Hack to reset position

seq.to_first();

for ( size_t i = 0 ; i < new_index ; ++i )

seq.next();

}

//Forward

std::ostream& operator<<(std::ostream& outs, DS::sequence& seq) {

//Save the current index so that we can restore it

size_t old_index = seq.current_index();

outs << '[';

if ( seq.is_item() )

outs << seq.current_value();

for ( seq.next() ; seq.is_item() ; seq.next() ) {

outs << ", " << seq.current_value();

}

outs << ']';

move_position_to(seq, old_index);

return outs;

}

//Reverse

std::ostream& operator>>(std::ostream& outs, DS::sequence& seq) {

//Save the current index so that we can restore it

size_t old_index = seq.current_index();

seq.to_last();

outs << '[';

if ( seq.is_item() )

outs << seq.current_value();

for ( seq.prev() ; seq.is_item() ; seq.prev() ) {

outs << ", " << seq.current_value();

}

outs << ']';

move_position_to(seq, old_index);

return outs;

}

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

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions

Question

Explain in detail the different methods of performance appraisal .

Answered: 1 week ago

Question

5. Have you any experience with agile software development?

Answered: 1 week ago