Question
HW19: Dynamic Memory Youre to write a program thatll prompt the user to enter the number of elements for an array. Then the user will
HW19: Dynamic Memory
Youre to write a program thatll prompt the user to enter the number of elements for an array. Then the user will supply the integer values. The objective is to display the array the user entered, sort the array from least to greatest, and then display the revised array. This will all be done using pointers and dynamic memory.
Sample run:
$ ./a.exe Enter the number of elements: 7 Enter 7 ints for the array: [1]: 7 [2]: 10 [3]: -3 [4]: 4 [5]: 2 [6]: -1 [7]: 12 The array you entered is: 7 10 -3 4 2 -1 12 The sorted array is: -3 -1 2 4 7 10 12 Releasing memory...
Main function will be responsible for getting the number of elements from the user. Then it will call the AllocArray function which will allocate a block space of memory (dynamic) and return the address of the location and store it to a pointer. It will then call the function InitArray which will prompt the user to fill the array with integer values. After, main will call the DispArray function to display the array the user entered. Then main will call the SortArray function, which will sort the array from least to greatest. Another call to DispArray will be made to show the updated results. Main should take care of releasing the memory allocated. Main function should be written in its own file called main.cpp
The functions below should be written in a file called DynMem.cpp
AllocArray will accept as argument an int value that has the number of elements the user desires. It will allocate a block space of memory and return the address.
InitArray will accept as argument an int pointer and an int value of the number of elements. It will prompt the user to enter a value to fill the array. It will return nothing.
DispArray will accept as argument an int pointer and an int value of the number of elements. It will display the array to the monitor. It will return nothing.
SortArray will accept as argument an int pointer and an int value of the number of elements. It will sort the contents of the array from least to greatest. It will return nothing.
Create a file called DynMem.h where you will only place the function prototypes.
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