using Dynamic_Array; /* * Dynamic Array for CSC3102 portfolio * - used to store any data type */ /* * --REQUIRED METHODS-- * DynamicArray() * Add(Atype item, int position) * Atype Remove(int position) * Atype Get(int position) * Atype Set(Atype val, int position) * int Size() * * --OPTIONAL METHODS-- * SizeUp() * ShiftUp(int pos) * ShiftDown(int pos) * AddEnd() * AddBeginning() * override ToString() OR printArray() */ namespace Dynamic_Array { public class DynamicArray //make sure format is correct { private int ArraySize; private int num_elements; public Atype[]? curArray; /*constructs an empty array * @ArraySize - size of the array will be 10 * @num_elements - since empty, it will have no elements * @curArray - it will exist */ public DynamicArray() { ArraySize = 10; num_elements = 0; curArray = new Atype[10]; } /* * adds an item to the specified index in the array, fails if index doesn't exist * @item - the item you want to add * @pos - the position (index) where you want to add it */ public void Add(Atype item, int pos) { if(pos <= num_elements && pos >= 0) { while (curArray == null || num_elements >= curArray.Length) //checks to see if item can fit in current array, increases sie if it can't { SizeUp(); } ShiftDown(pos); //creates space for item curArray[pos] = item; //adds item num_elements++; //increases number of elements in array by 1 } else if (pos < 0) //fails if position is less than 0 { throw new IndexOutOfRangeException("Position cannot be less than 0"); } else //fails if their is a space between the last index with a value, and attempted position to add a value { throw new IndexOutOfRangeException("Invalid position given"); } } /* * removes the item at the specified index, fails if index doesn't exist * @pos - the position (index) of the item you want removed */ public Atype Remove(int pos) { if (curArray != null && pos >= 0 && pos < num_elements) //will not do anything if current array is null or if position doesn't have an element { Atype value = curArray[pos]; ShiftUp(pos); num_elements--; return value; } throw new IndexOutOfRangeException("Position is out of range of elements"); } /* * return the value at specified index, fails if index doesn't exist * @pos - the position (ondex) of the item you want to retrieve */ public Atype Get(int pos) { if (curArray == null) { throw new IndexOutOfRangeException("The array is null"); } if(pos >= curArray.Length) //index does not exist in the array { throw new IndexOutOfRangeException("Index is out of range of the array"); } return curArray[pos]; } /* * sets the value of an item in an array to a new value, while returning the old value * @val - value of new item * @position - position (index) where new item will go */ public Atype Set(Atype val, int position) { if(curArray == null) { throw new IndexOutOfRangeException("The array is null"); } if (position >= curArray.Length) //index does not exist in array { throw new IndexOutOfRangeException("Position is out of range of the array"); } Atype item = curArray[position]; //stores previous item at that position as new variable curArray[position] = val; //updates position to val, which is the new item return item; //returns the old item } public int Size() { return num_elements; } /* * doubles the size of curArray */ void SizeUp() { if(curArray == null) //if there is no current array, we create one { new DynamicArray(); return; } Atype[] newArray = new Atype[curArray.Length * 2]; //doubles the size of the current array, stores it as new array for(int i = 0; i pos;i--) { curArray[i] = curArray[i - 1]; } } } //Prints the array, only the indexes that hold a value. //If an index within the supposed array's number of elements is null, it will print "(#-null)" //with # representing which index is null public void PrintArray() { Console.Write("["); for (int i = 0; i < num_elements; i++) { if (this.curArray[i] != null) { Console.Write(curArray[i] + " "); } else { Console.Write("(" + i + "-null) "); } } Console.WriteLine("]"); } } }