Question
Write a C++ Program for DMA & vectors: (Help is very much appreciated, thank you) /**************** Whenever you dynamically allocate memory using new, be sure
Write a C++ Program for DMA & vectors: (Help is very much appreciated, thank you)
/****************
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 delete[] .
**********************/
For this program, write a C++ program with the following functions:
void show( const int a[], unsigned elements );
void show(const vector
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 a[], unsigned els );
vector
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 a[], unsigned els );
vector
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.
For example, if a were {0, 5, 0, 0, -3, 0}, then nonzeroCopy would set nonzeroEls to 2
and return a pointer to (the first element of) {5, -3}.
Similarly for the vector version.
void changeSize( int * & ptr, int newEls, int oldEls );
void changeSize(vector
On entry, ptr points to a dynamically allocated array with oldEls elements (or ptr is NULL with oldEls equaling 0, which amounts to about the same thing).
changeSize's job is to
Dynamically allocate an array of newEls ints. (As always, c&d in the face of allocation failure.)
If newEls >= oldEls:
Copy the oldEls values from the old array (which ptr points to) to the new array
(which we just allocated).
Set the rest of the elements in the new array to 0.
Otherwise (if newEls < oldEls):
Copy newEls values from the old array to the new array.
Deallocate the old array (regardless of whether newEls is <= oldEls).
Make ptr point to the new array (that's why we made ptr a reference arg).
The result of this is that it seems to the caller as though his array magically changed size. (If you happen to be a C programmer, you may recognize that this function is
kinda sorta like the C realloc function.)
int main()
Your main should call each of the other 8 functions described at least once. You decide the details of how you want your main to work, but be sure that your main deallocates the memory that it should.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started