Question: EDIT: What's being requested is an updated code and screenshot for each task ( except for task 1 ) . I ' m sorry, but

EDIT: What's being requested is an updated code and screenshot for each task (except for task 1). I'm sorry, but that's all the information given. When I say terminal, I mean the terminal that is used for code, you can use another compiler. The parts of the code that tell you to insert in refer to changes you have to make in tasks. Sorry if it's confusing.
(Not hard but my terminal isn't working for me. Part 3-which is the last part so don't be worried- might require some thinking)
The following is Vector20Main.cpp and Vector20.h
Vector20Main.cpp
#include
#include
#include "Vector20.h"
using namespace std;
// printing values in a Vector20 object
void print_Vector20(Vector20);
int main()
{
Vector20 myvec;
for (int i =1; i <=25; i++)
{
myvec.push_back(i *11); // entering 25 values into myvec;
}
print_Vector20(myvec); // showing contents of myvec
// asking for target index and how many times an erase is to happen
// at this index;
/*-- UNCOMMENT FOR TASK 2---
int target;
int howmany;
cout << "Enter index of element to erase [0-"<< myvec.size()-1<<"]: ";
cin >> target;
cout << endl;
cout << "Erase at this index how many times [0-"<< myvec.size()- target
<<"]: ";
cin >> howmany;
cout << endl;
for (int i =1; i <= howmany; ++i)
{
myvec.erase(target);
print_Vector20(myvec);
cout << endl;
}
cout << endl;
*/
// asking for a target index and how many times value 999 is
// to be inserted at this index
/*-- UNCOMMENT FOR TASK 3--
cout << "Enter index of element to insert 999[0-"<< myvec.size()-1<<
"]: ";
cin >> target;
cout << endl;
cout << "Insert at this index how many times [0-"<< myvec.size()-1<<
"]: ";
cin >> howmany;
cout << endl;
for (int i =1; i <= howmany; ++i)
{
myvec.insert(target,999);
print_Vector20(myvec);
cout << endl;
}
*/
return 0;
}
void print_Vector20(Vector20 vec)
{
for (int i =0; i < vec.size(); ++i)
cout << vec[i]<<"";
cout << endl
<< endl;
}
Vector20.h
#ifndef VECTOR20_H
#define VECTOR20_H
#include
#include
using namespace std;
template
class Vector20
{
public:
Vector20()
: theSize{0}, theCapacity{20}
{
data = new T[theCapacity];
}
Vector20(const Vector20 &rhs)
: theSize{rhs.theSize}, theCapacity{rhs.theCapacity}, data{nullptr}
{
data = new T[theCapacity];
for (int k =0; k < theSize; ++k)
data[k]= rhs.data[k];
}
Vector20 &operator=(const Vector20 &rhs)
{
for (int k =0; k < theSize; ++k)
data[k]= rhs.data[k];
return *this;
}
~Vector20()
{
delete[] data;
}
bool empty() const
{
return theSize ==0;
}
int size() const
{
return theSize;
}
int capacity() const
{
return theCapacity;
}
T &operator[](int index)
{
assert(index >=0 && index < theSize);
return data[index];
}
T &back() const
{
return data[theSize -1];
}
T &front() const
{
return data[0];
}
void push_back(const T &x)
{
if (theSize < theCapacity)
{
// cout << "within capacity ..."<< endl;
data[theSize]= x;
theSize++;
}
else
{
// cout << "EXPAND capacity ..."<< endl;
T *moredata = new T[theCapacity *2+1];
for (int i =0; i < theSize; ++i)
{
moredata[i]= std::move(data[i]);
}
std::swap(data, moredata);
delete[] moredata;
theCapacity = theCapacity *2+1;
data[theSize]= x;
theSize++;
}
}
void pop_back()
{
--theSize;
}
// to be added in by you
// copy it in
void erase(int index)
{
// to be filled by you
return;
}
// to be added in by you
// to be written by you
void insert(int index, int value)
{
// to be filled in a task
return;
}
private:
int theSize;
int theCapacity;
T *data;
};
#endif
Your tasks are as follows:
Task 1:
Compile and run in a terminal using:
g++-o lab4 Vector20Main.cpp
./lab4
Task 2:
Insert the following erase() function
void erase (int index)
{
assert (index >=0 && index < theSize);
if (index == theSize -1)
{
pop_back();
return;
}
for (int i = index +1; i < theSize; i++)
{
data[i-1]= data [i];
}
pop_back();
return;
}
Test it with Vector20Main.cpp, and MAKE SURE you uncommented the part that says uncomment for task 2.After that, recompile and run again.
Task 3:
Here is an insert function:
void insert(iterator itrs, int value)
{
if (itz == endl())
{
push_back(value);
return;
}
assert(itr >= begin() && itr < endl());
push_back(back());
iterator itr1= end()-2;
iterator itr2= end()-1;
while (itr1>= itr)
{
* itr2=itr1;
itr1--;
itr2--;
}
*itr = value;
return;
}
Add to the class `Vector20` an `insert()` function. Given an index
(target) and a new value (x), the insert function is to insert the stated
value at the stated index. The one shown is just for you to get an idea on what to do. You will develop this on your own, as you must:
Add a copy of the vector's last (back) element using
`push_back`.
Beginning at index `theSize -2` and ending at the target index,
copy each element to the next index (`+1`).
Place the new value `x` at the target index.
2. Test your `insert()` function with the provided `Vector20Main.cpp` file
after uncommenting the relevant portion marked with "UNCOMMENT
FOR TASK 3."
3. Recompile and run
I sincerely appreciate your assistance.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!