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
  • 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;...

  • 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 <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...

  • 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...

  • 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...

  • Implementing the vector class in the following code (It's C++, not Java). You just need to...

    Implementing the vector class in the following code (It's C++, not Java). You just need to implement all these methods: PushFront, PopFront, At, Erase, Insert, Clear, Reserve, Copy, Assign, and Destroy, please do not add any new properties. You cannot use std:: functions, && (unless being used for "AND"), or pass by reference. Your solutions must conform to the Big O notations next to each method. In the following code, I am going to match the names that STL uses...

  • #include <iostream> #include <vector> #include <iomanip> using namespace std; int main() { const int NUM_ITEMS =...

    #include <iostream> #include <vector> #include <iomanip> using namespace std; int main() { const int NUM_ITEMS = 8; vector <double> inverse(NUM_ITEMS); int j; double temp; for (int i = 0; i < NUM_ITEMS; i++) { inverse.at(i) = 1 / (i + 1.0); } cout << fixed << setprecision(2); cout << "Original vector..." << endl; for (int i = 0; i < NUM_ITEMS; i++) { cout << inverse.at(i) << " "; } cout << endl; cout << "Reversed vector..." << endl; for...

  • 10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete...

    10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete main() to create a vector called myGarden. The vector should be able to store objects that belong to the Plant class or the Flower class. Create a function called PrintVector(), that uses the PrintInfo() functions defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to...

  • 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...

  • 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...

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
ADVERTISEMENT