In this lab, we review and practice arrays and functions. You will practice: - How to use array to store a collection of values:
- partially filled array (using a int variable to keep track of the number of elements)
- searching for a value in the array,
- inserting an element into the array,
- deleting an element from the array,
- reversing the array and using swap.
- How to clearly declare a function by writing concise function comments, and specifying properly name, parameters and return type for the function. This step address the question of what the function is supposed to do?
- Passing array as parameter to function: both the array and the number of elements need to be passed to the function
- Call-by-value and call-by-reference
- return type: for functions that perform some kind of checking, it should return bool type.
- How to implement a function given its declaration. This step takes care of How the function works? When implementing a function, all you need to know is the function's declaration.
Example Output: Enter a list of up to 20 integers or -1 to end the list 3 2 4 8 12 5 6 9 0 11 10 13 -1 3 2 4 8 12 5 6 9 0 11 10 13 Enter a value and a position to insert: 18 5 3 2 4 8 12 18 5 6 9 0 11 10 13 Enter a value to delete from the array: 18 3 2 4 8 12 5 6 9 0 11 10 13 Enter a value to append: 15 3 2 4 8 12 5 6 9 0 11 10 13 15 15 13 10 11 0 9 6 5 12 8 4 2 3 Please fill in the code in all places where there have a ToDo comment. Hints - Start with the simiplest function, e.g., the one to search the content of an array.
- The suggested order to write the function is:
- Function to search for a value in the array
- Function to fill an array with positive integers
- Function to delete an element from the array: after deletion, the array should have no holes.
- Function to insert an element into the array
- Write one function, test it to make sure it really works, and then move on to next function.
- Write comments to 1) document your algorithms and design, 2) make your code readable, 3) debug code.
#include using namespace std; const int CAPACITY=20; // [Already implemented] // displayArray - display the array on a single line separated by blanks. // @param: int array[] is an unordered array of integers // @param: int numberOfElements // [Already implemented] void displayArray(int array[], int numElements); //ToDo: Declare a function fillArray that fills an int array with values entered // by the user. Stop reading when the user inputs -1 or you reach CAPACITY. // fillArray - reads a list of positive integers ending with -1 or until CAPACITY. // @param: int array[] is an unordered array of integers when leaving this function // @param: int& numberElements is the number of Elements in the array after function // @returns void. //ToDo: Delcare a function that removes (i.e., deletes) the element // removeElement - removes the element of the given index from the given array. // @param: int array[] is an unordered array of integers // @param: int& numberElements // @param: int position of element to delete // @returns: true if delete was successful, false otherwise //ToDo: Delcare a function that inserts the element in the given position // insertElement - removes the element of the given index from the given array. // @param: int array[] is an unordered array of integers // @param: int& numberElements // @param: int position to insert into // @param: int target to insert. // @returns: true if insert was successful, false otherwise //ToDo: Declare a funcxtion that searches for an element in the given array // searchElement - searches for the element in the given array. // @param int array[] is an unordered array of integers // @param int numberOfElements // @param int element // @returns index of element or -1 if not found. // ToDo: Declare a function that reerses the array given an array and it's size. // @param int array[] is an unordered array of integers // @param int numberOfElements // @returns void, but array is reversed from the original order. // MUST USE a version of swap that you've implemented. int main() { // The NumArray can be partially filled, we use variable NumArrayElems to keep track of how many numbers // have been stored in the array. int NumArray[CAPACITY]; // an int array with a given CAPACITY int NumArrayElems=0; // the array is initially empty, i.e., contains 0 elements // 1. ToDo: Call your fillArray function to read in a sequence of integer values, // separated by space, and ending with -1. Store the values in the NumArray array // and the number of elements in NumArrayElems. // Display the contents of the array afterwards // 2. ToDo: Read in a value and position from the user. Call your insertElement function // to insert the given value into the given position of the array // Display the contents of the array afterwards // 3. ToDo: Read in a value and call your searchElement function. // 4. if the value is found, delete it from the array using your deleteElement function // if the value not found, print "Value not found!" // Display the contents of the array afterwards // 5. TODO: Read in a value and call your insertElement function to append // a value to the end of the array // Display the contents of the array afterwards // 6. TODO: Call your reverse function to reverse the values in the array. // Display the contents of the array. return 0; } //TODO: Implement all functions declared above. //Don't forget to put precondition/postcondition comments under or over the function header. | | |