Question: Array.h #ifndef ARRAY_H #define ARRAY_H #include #include #include #include Iterator.h namespace ds { namespace it { template class Iterator : public IteratorInterface { private: T*

Array.h
| #ifndef ARRAY_H | |
| #define ARRAY_H | |
| #include | |
| #include | |
| #include | |
| #include "Iterator.h" | |
| namespace ds | |
| { | |
| namespace it | |
| { | |
| template | |
| class Iterator : public IteratorInterface | |
| { | |
| private: | |
| T* data; | |
| int cnt; | |
| int capacity; | |
| public: | |
| Iterator() : Iterator(NULL,0) {} | |
| Iterator(T* data,int capacity) : data(data), capacity(capacity), cnt(0) | |
| { | |
| if(this->capacity | |
| { | |
| this->capacity = 0; | |
| } | |
| } | |
| bool HasNext() const | |
| { | |
| return (cnt | |
| } | |
| T& Next() | |
| { | |
| int c = cnt; | |
| cnt += 1; | |
| return data[c]; | |
| } | |
| }; | |
| } | |
| template | |
| class Array | |
| { | |
| private: | |
| T* data; | |
| int capacity; | |
| public: | |
| Array() : Array(100) {} | |
| Array(int capacity) : capacity(capacity) | |
| { | |
| if(this->capacity | |
| { | |
| this->capacity = 100; | |
| } | |
| data = new T[this->capacity]; | |
| for(int i = 0;i capacity;i += 1) | |
| { | |
| data[i] = T(); | |
| } | |
| } | |
| Array(const Array& obj) | |
| { | |
| /*Deep Copy*/ | |
| capacity = obj.capacity; | |
| data = new T[capacity]; | |
| for(int i = 0;i | |
| { | |
| data[i] = obj.data[i]; | |
| } | |
| } | |
| Array& operator=(const Array& rhs) | |
| { | |
| if(this != &rhs) | |
| { | |
| capacity = rhs.capacity; | |
| delete[] data; | |
| data = new T[capacity]; | |
| for(int i = 0;i | |
| { | |
| data[i] = rhs.data[i]; | |
| } | |
| } | |
| return *this; | |
| } | |
| ~Array() {delete[] data;} | |
| const T& operator[](int idx) const | |
| { | |
| if(idx = capacity) | |
| { | |
| throw "Out of bound"; | |
| } | |
| return data[idx]; | |
| } | |
| T& operator[](int idx) | |
| { | |
| if(idx = capacity) | |
| { | |
| throw "Out of bound"; | |
| } | |
| return data[idx]; | |
| } | |
| int Length() const {return capacity;} | |
| std::string ToString() const | |
| { | |
| std::stringstream out; | |
| out | |
| for(int i = 0;i | |
| { | |
| out | |
| if(i + 1 | |
| { | |
| out | |
| } | |
| } | |
| out | |
| return out.str(); | |
| } | |
| it::Iterator* ToIterator() const | |
| { | |
| return new it::Iterator(data,capacity); | |
| } | |
| friend std::ostream& operator | |
| { | |
| out | |
| return out; | |
| } | |
| }; | |
| } | |
| #endif |
C++ programming. Must use Array.h
Iterator.h
| #ifndef ITERATOR_H | |
| #define ITERATOR_H | |
| namespace ds | |
| { | |
| template | |
| class IteratorInterface | |
| { | |
| public: | |
| virtual bool HasNext() const = 0; | |
| virtual T& Next() = 0; | |
| }; | |
| } | |
| #endif |
Main.cpp
| #include | |
| #include | |
| #include | |
| #include "Array.h" | |
| void RandInts(ds::Array | |
| { | |
| int mx = (hi >= lo)?(hi):(lo); | |
| int mn = (lo | |
| ds::it::Iterator | |
| while(iter->HasNext()) | |
| { | |
| iter->Next() = rand() % (mx - mn + 1) + mn; | |
| } | |
| delete iter; | |
| } | |
| void SortedInts(ds::Array | |
| { | |
| int prev = start; | |
| int c = (dup)?(0):(1); | |
| ds::it::Iterator | |
| while(iter->HasNext()) | |
| { | |
| prev = prev + rand() % 10 + c; | |
| iter->Next() = prev; | |
| } | |
| delete iter; | |
| } | |
| int main() | |
| { | |
| ds::Array | |
| srand(time(NULL)); | |
| RandInts(a,1,50); | |
| SortedInts(b,8,false); | |
| std::cout | |
| std::cout | |
| return 0; | |
| } |
For this lab, your objective is to define functions that solve each array problem. You may only use the Array class from the lecture. Yo Given an array of integers and a target value, find two elements of the array that sums to the target. If the elements exist, display their indices and return true; otherwise, return false. The elements must be different. without rising another Given an array, num, and value, wil remove all instances of val from array. Furthermore, return the new length of the array, Given a sorted array and a target value return the index of the target if it is found; otherwise return the index where it would be if it were inserted in order. There may be duplicates. For this lab, your objective is to define functions that solve each array problem. You may only use the Array class from the lecture. Yo Given an array of integers and a target value, find two elements of the array that sums to the target. If the elements exist, display their indices and return true; otherwise, return false. The elements must be different. without rising another Given an array, num, and value, wil remove all instances of val from array. Furthermore, return the new length of the array, Given a sorted array and a target value return the index of the target if it is found; otherwise return the index where it would be if it were inserted in order. There may be duplicates
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
