Question

vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

vector.h:

#ifndef VECTOR_H

#define VECTOR_H

#include <algorithm>

#include <iostream>

#include <cassert>

template <typename T>

class Vector

{

    public:

        Vector( int initsize = 0 )

        : theSize( initsize ),

         theCapacity( initsize + SPARE_CAPACITY )

        { objects = new T[ theCapacity ]; }

        Vector( const Vector & rhs )

        : theSize( rhs.theSize),

         theCapacity( rhs.theCapacity ), objects( 0 )

        {

            objects = new T[ theCapacity ];

            for( int k = 0; k < theSize; ++k)

                objects[ k ] = rhs.objects[ k ];

        }

        Vector & operator = ( const Vector & rhs )

        {

            Vector copy(rhs);

            std::swap( *this, copy );

            return *this;

        }

        ~Vector() { delete [] objects; }

        bool empty() const { return size() == 0; }

        int size() const { return theSize; }

        int capacity() const { return theCapacity; }

        T & operator[]( int index )

        {

            assert(index >= 0 && index < theSize);

            return objects[ index ];

        }

        void resize( int newSize )

        {

            if( newSize > theCapacity )

                reserve( newSize * 2 );

            theSize = newSize;

        }

        void reserve( int newCapacity )

        {

            if( newCapacity < theSize )

                return;

            T *newArray = new T[ newCapacity ];

            for( int k = 0; k < theSize; ++k )

                newArray[ k ] = std::move(objects[k]);

            

            theCapacity = newCapacity;

            std::swap( objects, newArray );

            delete [ ] newArray;

        }

        void push_back( const T & x )

        {

        if( theSize == theCapacity )

            reserve( 2 * theCapacity + 1 );

        objects[ theSize++ ] = x;   

        }

        void pop_back( )

        {

            assert(!empty());

            --theSize;

        }

        

        const T & back () const

        {

            assert(!empty());

            return objects[ theSize - 1 ];

        }

        const T & front() const

        {

            assert(!empty());

            return objects[0];

        }

        void erase ( int k, int* it )

        {

            assert(!empty());

            for(int i=k; i<theCapacity; i++)

            {

                it[i]=it[i+1];

            }

        }

        void insert ( int k, T x )

        {

        }

        static const int SPARE_CAPACITY = 2;

    private:

        int theSize;

        int theCapacity;

        T * objects;

};

#endif

my main.cpp:

#include <iostream>

#include "Vector.h"

#include <vector>

using namespace std;

int max_subseq_sum_alg4 ( const vector<int> & vec1)

    {

        int maxSum = 0;

        int thisSum = 0;

        for (int i = 0; i < vec1.size(); i++)

        {

            thisSum += vec1[i];

            if (thisSum > maxSum)

                maxSum = thisSum;

            else if (thisSum < 0)

                thisSum = 0;

        }

        return maxSum;

    }

    void print ( const vector<int> & vec1 )

    {

        for( int i = 0; i < vec1.size(); i++)

        {

            cout << vec1.at(i) << " , ";

            

        }

    }

int main()

{

    vector<int> vec1;

    int next;

    int yourVectorElements;

    cout << "what is the size of your vector?";

    cin >> yourVectorElements;

    cout << endl;

    for (int i=0; i<= yourVectorElements-1; i++)

    {

        cout << "interger: ";

        cin >> next;

        cout << endl;

        

        vec1.push_back(next);

    }

    cout << endl;

    cout << max_subseq_sum_alg4(vec1);

    //cout << print(vec1);

    vec1.erase(k * 2);

    return 0;

}

need to check the implementaion of

1. the void print funtion which should print the vector.

2. void erase(int k){…} removes element at index k.

3. void insert(int k, T x){…} inserts the new element x at index k by moving all other elements to the right after insertion at index k.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

void erase(int k, int *it)

{

assert(!empty());

for (int i = k; i < theCapacity-1; i++)

{

it[i] = it[i + 1];

}

theSize--;

}

void insert(int k, T x)

{

if (theSize == theCapacity)

reserve(2 * theCapacity + 1);

for(int i=theSize-1; i>=k; i--)

objects[i+1] = objects[i];

objects[k] = x;

theSize++;

}

void print(){

for(int i=0; i<theSize; i++){

std::cout<<objects[i]<<" ";

}

std::cout<<std::endl;

}

Add a comment
Know the answer?
Add Answer to:
vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • //C++ program #include<iostream> using namespace std; template<typename T> class list{    private:        T*arr;   ...

    //C++ program #include<iostream> using namespace std; template<typename T> class list{    private:        T*arr;        int size;        int capacity;               void resize(){            capacity*=2;            T * newArr = new T[capacity];            for(int i=0;i<size;i++){                newArr[i] = arr[i];            }            delete(arr);            arr = newArr;                   }           public:       ...

  • C++ Implement the removeBad function: #include <list> #include <vector> #include <algorithm> #include <iostream> #include <cassert> using...

    C++ Implement the removeBad function: #include <list> #include <vector> #include <algorithm> #include <iostream> #include <cassert> using namespace std; vector<int> destroyedOnes; class Movie { public: Movie(int r) : m_rating(r) {} ~Movie() { destroyedOnes.push_back(m_rating); } int rating() const { return m_rating; } private: int m_rating; }; // Remove the movies in li with a rating below 50 and destroy them. // It is acceptable if the order of the remaining movies is not // the same as in the original list. void...

  • #include "stdafx.h" #include <iostream> #include <vector> #include <cassert> using namespace std; // function prototypes int loadDisk(vector<int>...

    #include "stdafx.h" #include <iostream> #include <vector> #include <cassert> using namespace std; // function prototypes int loadDisk(vector<int> &firstPeg, int numDisks); void printPeg(vector<int> &peg); int hanoi(struct pegType &startPeg, struct pegType &swapPeg, struct pegType &endPeg, int numDisk); void moveDisk(struct pegType &startPeg, struct pegType &endPeg); struct pegType {    vector <int> diskStack;    int name; }; int main() {    int numDisk = 7;    int i;    pegType peg1, peg2, peg3;    peg1.name = 1;    peg2.name = 2;    peg3.name = 3;...

  • Help finding my errors in C++ please Header file #ifndef _FISH_SHOWER_H #define _FISH_SHOWER_H #include <iostream> #include...

    Help finding my errors in C++ please Header file #ifndef _FISH_SHOWER_H #define _FISH_SHOWER_H #include <iostream> #include <string> using namespace std; //NOTE: Prototypes are correct, use as a guide void WriteHeader(); void FillVectors(vector<string> &type, vector<int> &minGals, vector<int> &maxGallons); void AskFishShowerTypes(vector<string> &type, int *pIndex); void CalcPondVol(int *pGal); bool ValidateFishShower(int index, int pondVol, vector<int> &minGals, vector<int> &maxGals, vector<int> &rec); void WriteInfo(string showerType, int gallons); void WriteInfo(string showerType, vector<string> &showerTypes, vector<int> &recommendations, int gallons); #endif Main file #include "FishShower.h" using namespace std; int main()...

  • I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string>...

    I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string> using namespace std; class Triple { private: int a, b, c; public: Triple(); // all elements have value 0 Triple(int k); // all elements have value k Triple(int x, int y, int z); // specifies all three elements Triple(string s); // string representation is "(a,b,c)" string toString(); // create a string representation of the vector void fromString(string s); // change the vector to equal...

  • 9. At your job, you are creating a library. A co-worker brought this test code to...

    9. At your job, you are creating a library. A co-worker brought this test code to you. They expect that the output would be "12 12 12 12 12". However, they are getting "Empty List" (a) Describe why the error is occurring. (b) Explain how to fix the code. #include <iostream> using namespace std; CON void increaseArray (int* array, int size, int value) { int newSize = size + 5; if (size ==0) { size = 5; O int* newArray...

  • Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int...

    Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

  • Given the following code: #ifndef TREE_H #define TREE_H #include <iostream> #include "TreeNode.h" template< typename NODETYPE > class Tree { public: Tree() : rootPtr( nullptr ) {}...

    Given the following code: #ifndef TREE_H #define TREE_H #include <iostream> #include "TreeNode.h" template< typename NODETYPE > class Tree { public: Tree() : rootPtr( nullptr ) {} void insertNode( const NODETYPE &value ) { insertNodeHelper( &rootPtr, value ); } void preOrderTraversal() const { preOrderHelper( rootPtr ); } void inOrderTraversal() const { inOrderHelper( rootPtr ); } private: TreeNode< NODETYPE > *rootPtr; void insertNodeHelper( TreeNode< NODETYPE > **ptr, const NODETYPE &value ) { if ( *ptr == nullptr ) * ptr = new...

  • #include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock...

    #include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock { private: std::chrono::high_resolution_clock::time_point start; public: void Reset() { start = std::chrono::high_resolution_clock::now(); } double CurrentTime() { auto end = std::chrono::high_resolution_clock::now(); double elapsed_us = std::chrono::duration std::micro>(end - start).count(); return elapsed_us; } }; class books{ private: std::string type; int ISBN; public: void setIsbn(int x) { ISBN = x; } void setType(std::string y) { type = y; } int putIsbn() { return ISBN; } std::string putType() { return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT