CMPS-2020 Programming II: Data Structures and Algorithms
Lab-4
Overview:
Write a program: 2020/4/lab4.cpp on Odin. You may start with your lab1.cpp program, as it already contains a menu. When you start your program it should look like this...Lab-4 Menu of options ---------------------- 1. Search an array 2. Allocate memory 3. Allocate memory template 4. Display an array q. Quit the programThe programming assignments below come from our textbook chapter-16 problems 24, 25, 26, 27. Programming instructions... 1. Write a function that searches a numeric array for a specified value. The function should return the subscript of the element containing the value if it is found in the array. If the value is not found, the function should throw an exception. Your program will catch the exception and produce some output that explains to the user what happened. 2. Write a function that dynamically allocates a block of memory and returns a char pointer to the block. The function should take an integer argument that is the amount of memory to be allocated. If the new operator cannot allocate the memory, the function should return a null pointer. note: this function accepts just one argument. calling a template function 3. Make the function you wrote in problem #2 a template. Your template function should allocate memory for any primitive data type. 4. Write a template for a function that displays the contents of an array of any type.
How to test your code... #1. Define an array and fill it with values. Display the array values. Prompt the user to enter a value. Search for the value, and display the results. #2. Define a character pointer. Allocate memory for the pointer using your function. Ask the user to enter a word or sentence that is stored using your pointer. Then display the string to show it works. To make this function work properly, you will have to use a try/catch block and throw an exceptions on an error. The try, catch, and throw should be inside your function. The function does not print any messages to the screen. You must prompt the user for the amount of memory to allocate before they enter their string. Our computers are running a 64-bit operating system, so store the amount of memory needed in a integer variable type that is 64-bits in size. #3. Write a different allocate function from problem #2, and make this one a template function. Demonstrate it similar to problem #2. #4. Use an array that is small in size so that you can display it and see the contents neatly on the screen.
Look in our textbook chapter 16, and find Programming Challenges 3, 4, and 5. These challenges ask you to write template functions. 1. Write the template functions. 2. Add one or more items to your lab4 menu to demonstrate your functions. Show that the functions work.
Your instructor will find your work on the Odin server.