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

Please help with following C++ coding questions 1. Design a \"person\" struct wh

ID: 3672394 • Letter: P

Question

Please help with following C++ coding questions

1. Design a "person" struct which contains data and or methods that a "person" might need. Write an in-place body swapper. This function should allow you to have two people switch some attributes in-place.

2. Make your own dynamic array template. It should allow creating contiguous arrays (filled with things of the same type) which you can extend without worrying about running out of space. Do one version using malloc and free. Do one version using new and delete.

Please post the pictures of results.

Explanation / Answer

2)

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
template<typename T>
class Array
{
public:
Array(int size_arg)
{
size = size_arg;
data = new T[size_arg];
};
Array<T> *addData(T dataToAdd)
{
Array <T> *tmp = new Array <T> (this->size);
tmp->data = this->data;
Array <T> *newData = new Array<T> (this->size + 1);
for (int i = 0; i < this->size + 1; ++i){
if (i < this->size)
{
//newData->data[i] = tmp->data[i];
newData->setData(tmp->getData()[i], i);
}
else
{
//newData->data[i] = dataToAdd;
newData->setData(dataToAdd, i);
}
}
return newData;
};
void initArray()
{
for (int i = 0; i < this->size; ++i)
{
//this->data[i] = i;
this->setData(i, i);
}
};
void printArray()
{
ostringstream oss;
string answer = "";
for (int i = 0; i < this->size; ++i)
{
oss << this->data[i] + " ";
//cout << this->data[i] << " ";
}
answer = oss.str();
cout << answer << "asdf" << endl;
};
T* getData()
{
return this->data;
}
int getSize()
{
return this->size;
}
void setData(T data, int index)
{
this->getData()[index] = data;
}
private:
int size;
T* data;
};