Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please C++ programming 6. The following CH main driver, along with function myFunc, uses a CH class DynArray that models a dynamic integer array that

Please C++ programming image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
6. The following CH main driver, along with function myFunc, uses a CH class DynArray that models a dynamic integer array that is, the class uses dynamic memory allocation to create a contiguous block of memory for storing a specified number of integers. The indexing for a DynArray object is the same as for a regular array. But, a DynArray can be initialized to size zero. Write the CH DynArray class. Here is a brief description of all of the class functions that your class should include: No-argument constructor - initializes a DynArray object to being empty. One-argument constructor - uses dynamic memory allocation to obtain a contiguous block of memory for storing n int values, where n is its argument. show - displays the nth element in the DynArray. If the DynArray is empty or ifn is an invalid index, this function should generate an error message. set - will set the nth element in the DynArray to x, where n is the value of its first argument and x is value of its second argument. If the DynArray is empty or if n is an invalid index, this function should generate an error message. expand - will take an existing DynArray and expand its size by its argument, s. Hint: To expand a DynArray, allocate a new larger block of dynamic memory, copy the values from the old DynArray to the new memory, and deallocate the old memory. A destructor to deallocate dynamic memory when a DynArray object passes out of scope. Requirement: When accessing the dynamic array elements in the set, show and expand member +, along with pointer arithmetic instead of the Requirement: When accessing the dynamic array elements in the set, show and expand member fmetions, you must use the dereferencing operator, -, along with pointer arithmetic instead of the array indexing operator, 1 Next, combine your DynArray class with the following main and my Func code and run the resulting CH program. The output generated from a run of your program should be similar to that shown in the output of a sample run given after the code. void my Eunc(); int main() + int size, more, i; DynArray y: cout > size: DynArray x(size); for(i=0;i> more; x.expand (more) : for (i=0;i> ch: return 0; //invalid index in shell vozd myFunc) void myrune int 12 cout Output from a sample run of the program (user input is in bold): Enter dynamic array size: 3 0 3 6 How much more dynamic array space do you want? 2 1 5 10 IS 20 Invalid Index in show Invalid index a set Cannot set DynArray empty cannot show - DynArray empty D.fo ) Output from a sample run of the program user input is in bold): Enter dynamic array size: 3 0 3 6 How much more dynamic array space do you want? 2 0 5 10 15 20 Invalid index in show Invalid index in set Cannot set DynArray empty Cannot show DynArray empty hi from my Func... 0 I I 9 16 bye from myFunc... hi from the DynArray destructor

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions