C++ Progam ONLY Description: Develop a program that mimics some of the functiona
ID: 3889220 • Letter: C
Question
C++ Progam ONLY
Description:
Develop a program 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:
1.) 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.
2.) 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.
3.) 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.
4.) clear(double *&arr, int &size)
Clears all elements of the array (arr) and updates the size (size) to be 0.
5.) 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.
6.) 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.
7.) init(double *arr, int size)
Populates the elements of the array (arr) with input from the user (or via file redirection).
8.) print(double *arr, int size)
Prints the elements of the array.
Additional Specifications:
Your program should not use any pre-existing classes such as string or vector classes!
NO GLOBAL VARIABLES!
Each function needs to be properly commented.
Make sure your program compiles and runs on one of the Linux machines.
Your program should consist of one source file Array.h. They must be named exactly as indicated.
Example Output:
casey@vanderwaal:-/Dropbox/Teaching/CSCE240Fal12017/Projects/Assignment3s ./a.out Please enter the size of your array: 5 Please enter 5 elements to populate the array. 1 2 3 45 The original array: 1 2 3 45 Size: 5 Index is not within the bounds of n+1. The new array: 1 9.3 2 3 45 Size: 6 Index is not within the bounds of n. The new array: 1 9.3 2 3 4 Size: 5 The element at position 2 is: 2 The element at position 10 is: -1 9.3 was found at position: 1 999 was found at position: 1 The arr and arr2 are not equal. casey@vanderwaal:-/Dropbox/Teaching/CSCE240Fall2017/Projects/Assignment3sExplanation / Answer
#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_;
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.