Develop a program in C++ that mimics some of the functionalities of an ArrayList
ID: 3888878 • Letter: D
Question
Develop a program in C++ that mimics some of the functionalities of an ArrayList in Java. Your program should maintain a pointer array of doubles and be able to perform the following functions:
-insert(int index, double num, double *&arr, int &size):
Adds an element (num) to the array (arr) at a given position (index) and updates the size. You may allow the user to add to the immediate end of the array (at position n+1 for an array of n elements) but not past. You should print an error message if they try to print beyond these bounds.
- remove(int index, double *&arr, int &size):
Removes an element from the array (arr) at a given position (index) and updates the size.
If index is out of the bounds of the array then an error message should be printed.
-get(int index, double *arr, int size):
Returns the element at the given position (index).
Should check if the index given is outside the bounds of the array. If it is out of bounds an error message should be printed.
-clear(double *&arr, int &size):
Clears all elements of the array (arr) and updates the size (size) to be 0.
-find(double num, double *arr, int size):
Returns the first index in which a given element (num) is found in the array (arr). If not found -1 is returned.
-equals(double *arr1, int size1, double *arr2, int size2):
Returns true if the contents of the two arrays are equal and false if they are not equal.
-init(double *arr, int size):
Populates the elements of the array (arr) with input from the user (or via file redirection).
-print(double *arr, int size):
Prints the elements of the array.
Explanation / Answer
main.cpp
#pragma once
template <typename T>
class ArrList {
public:
ArrList () : ArrList(8) {}
explicit ArrList (int cap) : cap_(8), sze_(0) {
while (cap > cap_) {
cap_ *= 2;
}
dat_ = new T[cap_];
}
ArrList (ArrList const& thiss) : cap_(thiss.cap_), sze_(thiss.sze_) {
dat_ = new T[cap_];
memcpy(dat_, thiss.dat_, sizeof(T) * sze_);
}
ArrList (ArrList&& thiss) : cap_(thiss.cap_), sze_(thiss.sze_) {
dat_ = thiss.dat_;
thiss.dat_ = nullptr;
}
~ArrList() {
delete[] dat_;
}
ArrList& op= (ArrList const& thiss) {
cap_ = thiss.cap_;
sze_ = thiss.sze_;
delete[] dat_;
dat_ = new T[cap_];
memcpy (dat_, thiss.dat_, sizeof(T) * sze_);
return *this;
}
ArrList& op= (ArrList&& thiss) {
cap_ = thiss.cap_;
sze_ = thiss.sze_;
delete[] dat_;
dat_ = thiss.dat_;
thiss.dat_ = nullptr;
return *this;
}
int sizes() const {
return sze_;
}
int cap() const {
return cap_;
}
void adding (T const& val) {
if (sze_ + 1 <= cap_) {
dat_[sze_] = val;
++sze_;
}
else {
cap_ *= 2;
T* newDat_ = new T[cap_];
memcpy(newDat_, dat_, sizeof(T) * sze_);
delete[] dat_;
dat_ = newDat_;
dat_[sze_] = val;
++sze_;
}
}
void insert (int indice, T const& val) {
if (indice <= sze_) {
if (sze_ + 1 <= cap_) {
memmove (dat_ + indice, dat_ + indice - 1, sizeof(T) * (sze_ - indice + 1));
dat_[indice - 1] = val;
++sze_;
}
else {
cap_ *= 2;
T* newDat_ = new T[cap_];
memmove (newDat_, dat_, sizeof(T) * (indice - 1));
memmove (newDat_ + indice, dat_ + indice - 1, sizeof(T) * (sze_ - indice + 1));
delete[] dat_;
dat_ = newDat_;
dat_[indice - 1] = val;
++sze_;
}
}
}
template <typename Itt>
void insert (int indice, Itt frst, Itt lst) {
if (indice <= sze_) {
int dus = distance (frst, lst);
if (sze_ + dus <= cap_) {
memmove (dat_ + indice + dus - 1, dat_ + indice - 1, sizeof(T) * (sze_ - indice + 1));
memcpy(dat_ + indice - 1, frst, sizeof(T) * dus);
sze_ += dus;
}
else {
while (cap_ < sze_ + dus) {
cap_ *= 2;
}
T* newDat_ = new T[cap_];
memmove (newDat_, dat_, sizeof(T) * (indice - 1));
memmove (newDat_ + indice + dus - 1, dat_ + indice - 1, sizeof(T) * (sze_ - indice + 1));
delete[] dat_;
dat_ = newDat_;
memcpy(dat_ + indice - 1, frst, sizeof(T) * dus);
sze_ += dus;
}
}
}
void cleared () {
sze_ = 0;
}
void rem (int indice) {
if (indice <= sze_) {
memmove (dat_ + indice - 1, dat_ + indice, sizeof(T) * (sze_ - indice));
--sze_;
}
}
template <typename Itt>
void rem (Itt frst, Itt lst) {
int bef = distance (dat_, frst);
int dus = distance (frst, lst);
if (bef + dus > sze_) {
lst = ending();
dus = sze_ - bef;
}
int after = distance (dat_, lst);
memmove (dat_ + bef, dat_ + bef + dus, sizeof(T) * (sze_ - after));
sze_ -= dus;
}
void reserving (int newCapacity) {
if (newCapacity > cap_) {
do {
cap_ *= 2;
} while (newCapacity > cap_);
T* newDat_ = new T[cap_];
memcpy (newDat_, dat_, sizeof(T) * sze_);
delete[] dat_;
dat_ = newDat_;
}
}
T* beginning () {
return dat_;
}
T const* beginning () const {
return dat_;
}
T* ending () {
return dat_ + sze_;
}
T const* ending () const {
return dat_ + sze_;
}
private:
int cap_;
int sze_;
T* dat_;
};
Rate an upvote....Thankyou
Hope this helps....
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.