I have to make my own array class with the following requirements. Array class:
ID: 3750275 • Letter: I
Question
I have to make my own array class with the following requirements.
Array class:
- must be templated
- must be dynamic (i.e., able to grow or shrink as required)
- Array(); create an empty Array object with slots for 10 initial items
- void append(): add new items after the last item in the Array
- int size() const: return the number of items currently in the array (not the capacity!)
- T operator[](int): retrieve the item by index position
- must perform run-time array-out-of-bounds checking for all access operations
How do i implement the void append() function? Below is my code, this is a c++ problem.
template <class elemType>
class Array{
Array();// create an empty Array object with slots for 10 initial items
void append(); //add new items after the last item in the Array
int size() const;// return the number of items currently in the array (not the capacity!)
elemType operator[](int);// retrieve the item by index position
// - must perform run-time array-out-of-bounds checking for all access operations
elemType *array[];
int length.
};
template <class elemType>
Array < elemType> :: Array(){
length = 10;
array = new elemType[length];
for (int i=0; i<length; i++) {
array[i] = 0; // Initialize all elements to zero.
}
}
template <class elemType>
int Array <class elemType>::size() const{
Array a;
int count=0,i=0;
elemType v = NULL;
for (i; i < length; i++){
if(a[i] != v)
count++;
else break;
return count;
}
template <class elemType>
Array <elemType> :: operator[](int i){
if (i > length)
throw 1;
return array[i];
}
Explanation / Answer
I had already created the same code before, just copy the required function from my code and add those function to your class add template and remove parameters of function and rename arr to array, and size to length.
please give thumbs up, thanks, if you face any problem during template or addind function, omment, i will show the demo
for any query please comment.
code:
#include <iostream>
#include <string>
#define nullptr 0
using namespace std;
//used to know how many elemnets are currently in array/List
int counter = 0;
//PRINT ELEMENTS FUNCTION
void printElements(int arr[])
{
//from 0 to number of elements-1 display arrays content
for (int i = 0; i < counter; i++)
{
//if current index contains element, then add , so all data will dislayed in formated manner
if (i != (counter - 1))
{
cout << arr[i] << ", ";
}
else
{
cout << arr[i];
}
}
cout << endl;
}
//EXPAND SIZE FUNCTION
void expandArray(int* &arr, int &size)
{
//creating integer point to set to null
int *tempPtr = nullptr;
//if list is full, then expand its size to double
if (counter == size)
{
//then create a new array with multiple of size 2
tempPtr = new int[size * 2];
//copy each element to array to newly created array
for (int i = 0; i < counter; i++)
{
tempPtr[i] = arr[i];
}
//delete old array
delete[] arr;
//make old array to point null i.e nothing
arr = nullptr;
//pass address of newly created array is pointer arr
arr = tempPtr;
//increase size variable, beacuase size is doubled now
size = size * 2;
//as new array is now pointed by arr, so make pointer tempptr to null
tempPtr = nullptr;
}
}
//ADD ELEMENTS FUNCTION
void addElement(int* &arr, int &size)
{
int userAddElement;
int temp;
bool Added = false;
bool exp = false;
//is array is full then exapnd array to double by calling function exapandArray and make flag exp to true
if (counter == size)
{
expandArray(arr, size);
exp = true;
}
//sort content to array
for (int i = 0; i < counter - 1; i++)
{
for (int j = 0; j < counter - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
//prompt user and ask for input
cout << "Add element: ";
//take input int inputelement
cin >> userAddElement;
//if array is doubled then display message
if (exp)
{
cout << "Array Expanded" << endl;
}
cout << endl;
//if array has no element then add to first index, and increase counter so indicate that new element is added
if (counter == 0)
{
arr[0] = userAddElement;
counter++;
}
//if list has aready one or more element
else
{
//add element as depend on your condition
for (int i = 0; i < counter; i++)
{
if (userAddElement < arr[i])
{
//if input is less then element pointed by index i move each element to right , i.e right shift and make added flag to true
for (int j = counter - 1; j >= i; j--)
{
arr[j + 1] = arr[j];
}
arr[i] = userAddElement;
Added = true;
counter++;
break;
}
}
//if element is not added then add at last index, and increment counter
if (Added == false)
{
arr[counter] = userAddElement;
counter++;
}
}
}
//SHRINK SIZE FUNCTION
void shrinkArray(int* &arr, int &size)
{
bool shrunk = false;
int *tempPtr = nullptr;
//while current number of elements is less than half of size of array
while (counter < (size / 2))
{
//create new array is half size
tempPtr = new int[size / 2];
copy each element to new array
for (int i = 0; i < counter; i++)
{
tempPtr[i] = arr[i];
}
//delete privious array
delete[] arr;
arr = nullptr;
//ad address of new array to pointer arr which hold the address of actuall array
arr = tempPtr;
size = size / 2;
shrunk = true;
tempPtr = nullptr;
}
//if array is made half show message that memry is shrunkedd
if (shrunk == true)
{
cout << "Array size Shrunk to: " << size << endl << endl;
}
}
//DELETE ELEMENTS FUNCTION
void deleteElement(int* &arr, int &size)
{
int userDeleteElement;
bool Found = false;
//take input from element to delete that from array
cout << "Delete element: ";
cin >> userDeleteElement;
cout << endl << endl;
//if arrat contains element
if (counter != 0)
{
//for each element
for (int i = 0; i < counter; i++)
{
//if ith position contains element that we need to delete
if (arr[i] == userDeleteElement)
{
//then set found flag to true
Found = true;
//move all right element from i index to left shift
for (int j = i; j < counter; j++)
{
arr[j] = arr[j + 1];
}
i = -1;
counter--;
}
}
//if element not found then show error
if (Found == false)
{
cout << "Error: " << userDeleteElement << " was not found in the array." << endl << endl;
}
}
//if not contains an element then display error
else
{
cout << "ERROR: There are no elements in the array to delete. " << endl << endl;
}
shrinkArray(arr, size);
}
//RETURN SIZE FUNCTION
void returnSize(int size)
{
cout << "S: " << size << ", E: " << counter << endl;
}
//THE MAIN FUNCTION
int main()
{
//creating a variable to store array size
int size = 2;
//creating a integer pointer which hold address to an integer array of size "size"
int *mainArray = new int[size];
//variable to store user input
string userInput;
string trash;
do
{
//printing menu
cout << "p) Print Elements" << endl;
cout << "a) Add Element" << endl;
cout << "d) Delete Element" << endl;
cout << "r) Return Size" << endl;
cout << "e) Exit" << endl << endl;
cout << " Enter Option: ";
//taking input in uerinput using function getline
getline(cin, userInput);
cout << endl << endl;
//if user enters p then call printElement function to display elements in array
if (userInput == "p")
{
printElements(mainArray);
}
//if user enters s then call addelement function which will add take data from use and insert to array
else if (userInput == "a")
{
addElement(mainArray, size);
}
//if user enter d then delete
else if (userInput == "d")
{
deleteElement(mainArray, size);
}
//if user input r then display array's size and how many elements it contains
else if (userInput == "r")
{
returnSize(size);
}
//if user enter e then exit while loop
else if (userInput == "e")
{
break;
}
//if user enter anything else, then show error
else
{
cout << "Your input is not an option on the menu." << endl << endl;
}
getline(cin, trash);
} while (userInput != "e"); //continue while loop while user not enters e
//then deleter array, to memory can be free
delete[] mainArray;
//make pointer to null
mainArray = nullptr;
return 0;
}