Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

do this in c++ and explain what you are doing! 1 Introduction In this lab we wil

ID: 3796472 • Letter: D

Question

do this in c++ and explain what you are doing!

1 Introduction

In this lab we will create a simplified version of the Standard Template Library’s vector class. We will call our class

sequence to avoid confusion. (Note: This is not the sequence class discussed in your textbook). A sequence holds

a collection of values. Values may be added and removed from the sequence. The values in a sequence are associated

with an index but a value’s index may change as items are added or removed from the sequence.

2 Walk through

Below is a sample program that demonstrates the effect of most of the operations that we can perform on a sequence.

The comments show the conceptual state of the sequence after each operation. Make sure you understand this code

before you proceed. Note that sequence indices start at zero (just like arrays).

s e q u e n c e s 1 ; / / [ ]

s 1 . add ( 5 ) ; / / [ 5 ]

s 1 . add ( 9 ) ; / / [ 5 , 9 ]

s 1 . add ( 2 ) ; / / [ 5 , 9 , 2 ]

s 1 . i n s e r t ( 1 , 1 5 ) ; / / [ 5 , 1 5 , 9 , 2 ]

s 1 . remove ( 0 ) ; / / [ 1 5 , 9 , 2 ]

c o ut << s 1 . g e t ( 0 ) << e n dl ; / / would p r i n t 15

c o ut << s 1 . s i z e ( ) << e n dl ; / / would p r i n t 3

s 1 . i n s e r t ( 0 , 3 ) ; / / [ 3 , 1 5 , 9 , 2 ]

s 1 . i n s e r t ( 0 , 2 2 ) ; / / [ 2 2 , 3 , 1 5 , 9 , 2 ]

f o r ( i n t i = 0; i < s 1 . s i z e ( ) ; i ++ )

c o ut << s 1 . g e t ( i ) << ” ” ; / / p r i n t s 22 3 15 9 2

s e q u e n c e s 2 ;

s 2 . add ( 7 ) ; / / s 2 = [ 7 ]

s 2 . add ( 5 ) ; / / s 2 = [ 7 , 5 ]

s e q u e n c e s 3 = s 1 + s 2 ; / / s 3 = [ 2 2 , 3 , 1 5 , 9 , 2 , 7 , 5 ]

s e q u e n c e s 4 ;

s 4 . add ( 1 ) ; / / s 4 = [ 1 ]

s 4 += s 2 ; / / s 4 = [ 1 , 7 , 5 ]

3 Files

Create files for a class called sequence and associated unit tests (sequence tests.cpp).

4 Unit tests

Try to force yourself to work “test first” through this class. That is, write a test, verify that it fails, make it pass. Don’t

write code in your sequence class until you have a test that needs it. Begin by writing a test that ensures that a sequence

is empty when it is first created. Next write a test that checks that adding an item increases the size of the sequence.

Keep moving forward in small steps. See the sections below for lists of required methods and instance variables.

5 Instance variables

You will need to mimic the bag class’ value type and CAPACITY features. Place them in your class.

As with the bag class we will need:

• value type items [CAPACITY] – the array where we store the items in the list

• std :: size t used – the number of items being used in the array

6 Operations

Implement the operations below. Use assert to check preconditions.

• sequence () – construct an empty sequence

• std :: size t size () const – return the number of items stored in the sequence

• value type get ( std :: size t i ) const – (precondition: i < size () ) return the item at position i in the sequence.

Note that sequence indices start at zero (just like arrays).

• void add(const value type & value) – (precondition: size () < CAPACITY) add the specified value to the end

of the sequence

• void insert ( std :: size t index, const value type & value) –

(precondition: index <= size () && size() < CAPACITY) insert the specified value at the specified index,

shifting values to higher indexes if needed. If index == size this operation is the same as add.

• void remove(std :: size t index) – (precondition: index < size () ) remove the value at the specified index,

shifting other values down as needed.

• sequence operator +(const sequence& s1, const sequence& s2) –

(precondition: s1. size () + s2. size () <= CAPACITY) free function (you may implement this as a member

function if you prefer) return a new sequence that is the result of appending s2 on to the end of s1

• void operator +=(const sequence& other) – (precondition: size () + other . size () <= CAPACITY append other onto the end of this sequence

Explanation / Answer

PROGRAM CODE:

#include <iostream>
#include <string.h>
using namespace std;

#define CAPACITY 40

template <class T>
class Sequence
{
   T *values;
   size_t SIZE;
   public:
   //constructor
   Sequence()
   {
       SIZE = 0;
       values = new T[CAPACITY];
   }
   //getting the size of the array
   size_t size()
   {
       return SIZE;
   }
   //getting an element at particular index
   T get(size_t index) const
   {
       if(index>=0 && index<CAPACITY)
           return values[index];
   }
   //appending an element ot the end of the array
   void add(const T & value)
   {
       if(SIZE<CAPACITY)
           values[SIZE++] = value;
   }
   //inserting an element to the particular position
   void insert (size_t index, const T & value)
   {
       if(SIZE<CAPACITY)
       {
           if(index==SIZE)
               add(value);
           else
           {
               if(index<SIZE )
               {
                   T temp1, temp2;
                   temp2 = value;
                   for(int i=index; i<SIZE; i++)
                   {
                       temp1 = values[i];
                       values[i] = temp2;
                       temp2 = temp1;
                   }
                   values[SIZE++] = temp2;
               }
           }
       }
   }
   //removing an element from a particular index
   void remove(size_t index)
   {
       if(index<SIZE)
       {
       for(int i = index; i<SIZE-1; i++)
       {
           values[i] = values[i+1];
       }
       values[SIZE] = NULL;
      
       SIZE--;
       }
   }
   //adding to the existing array
   void operator +=(const Sequence<T>& other)
   {
       if(other.size() + size() < CAPACITY )
       {
           for(int i=0; i<other.size; i++)
           {
               add(other.get(i));
           }
       }
   }
   //adding two arrays and returnin a new array
   friend Sequence<T> operator+( Sequence<T>& s1, Sequence<T>& s2)
   {
       Sequence<T> s3;
       if(s1.size() + s2.size() < CAPACITY)
       {
           for(int i=0; i<s1.size(); i++)
           {
               s3.add(s1.get(i));
           }
           for(int j=0; j<s2.size(); j++)
           {
               s3.add(s2.get(j));
           }
       }
       return s3;
   }
   //printing the whole array to the console
   void print()
   {
       cout<<"[";
       for(int i=0; i<SIZE; i++)
       {
           cout<<values[i];
           if(i<SIZE-1)
               cout<<", ";
       }
       cout<<"]"<<endl;
   }
};
int main() {
   //instantiation
   Sequence<int> s1, s2, s3;
   //adding ot the end of the sequence
   s1.add(21);
   //printing all the values
   s1.print();
   //adding more values
   for(int i=0; i<10; i++)
       s1.add(i);
   s1.print();
   //removing from index 3
   s1.remove(3);
   s1.print();
   //inserting at [position] = 4
   s1.insert(4, 34);
   s1.print();
   cout<<"Size of s1: "<<to_string(s1.size())<<endl;
   //Anaother Sequence
   s2.add(43);
   s2.print();
   //Adding two sequences
   s3 = s1 + s2;
   s3.print();
   return 0;
}

OUTPUT: