Question: Write a C++ program for the following problem: (HELP is very much appreciated) Whenever you dynamically allocate memory using new, be sure you Put the

Write a C++ program for the following problem: (HELP is very much appreciated)

Write a C++ program for the following problem: (HELP is very much

appreciated) Whenever you dynamically allocate memory using new, be sure you Put

Whenever you dynamically allocate memory using new, be sure you Put the new expression in a try-catch block. Catch the bad_alloc exception in the catch phrase, and c&d in the face of allocation failure. Eventually deallocate the memory (unless the program c&d's, in which case we don't care). Depending on the logic of the program, the deallocation might be in the same function as the allocation, or a different function. Whenever you deallocate dynamically allocated memory with delete, make sure you: Don't subsequently use the memory you have deallocated. Don't deallocate the same memory allocation more than once. Pay attention to whether the deallocation should be delete or deletell For this program, write a C++ program with the following functions: void show const int al, unsigned elements ; void show(const vector & v) show's job is to output the values in the arg array. You decide the formatting details. Likewise for the vector version int copy const int al, unsigned els ) vector copy(const vector int> & v); copy's job is dynamically allocate an array of els ints, copy the values from the arg array to the dynamically allocated array, and return a pointer to (the beginning of) the allocated array. Similarly for the vector version. Your main should create an array of ints, call copy to get a copy of the array, sort the dynamically allocated array (you decide how) leaving the original array unchanged output the sorted array, and then deallocate the sorted array int nonzeroCopy unsigned & nonzeroEls, const int al, unsigned els ); vectorcint> nonzeroCopy(const vector & v); nonzeroCopy's job is similar to copy's. els is the number of elements in a. nonzeroCopy counts how many elements in a are nonzero, dynamically allocates an array that's just big enough to hold the nonzero elements of a, fills the dynamically allocated array with copies of the nonzero elements of a, sets nonzeroEls to the number of nonzero elements, and returns a pointer to the dynamically allocated array. Deallocation, of course, is the caller's responsibility On entry, nonzeroEls is garbage

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!