HW19: Dynamic Memory You’re to write a program that’ll prompt the user to enter
ID: 3720656 • Letter: H
Question
HW19: Dynamic Memory
You’re to write a program that’ll 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:
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.
Explanation / Answer
#include "DynMem.h"
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<" Enter Number of Element for an Array : ";
cin>>n;
int *arr=AllocArray(n);
InitArray(arr,n);
cout<<" ";
DispArray(arr,n);
SortArray(arr,n);
cout<<" After Sorting ";
DispArray(arr,n);
return 0;
}
int *AllocArray(int n)
{
int *array=new int[n]; //Allocate n block memory space of type int;
return array; //retrun address
}
void InitArray(int *arr,int n)
{
cout<<" Enter Array Element : ";
for(int i=0;i<n;i++)
cin>>arr[i]; //store Elements of Array Entered by user
}
void DispArray(int *arr,int n)
{
cout<<"Array Elements Are : ";
for(int i=0;i<n;i++) //Display Elements
cout<<arr[i]<<" ";
}
//Selection Sort
void SortArray(int *arr,int n)
{
int i,j,min_ind,temp;
for(i=0;i<n-1;i++)
{
min_ind=i;
//find minimum element in unsorted array
for(j=i+1;j<n;j++)
{
if(arr[j]<arr[min_ind])
{
min_ind=j;
}
}
// swap minimum element with first element of unsorted array
temp=arr[i];
arr[i]=arr[min_ind];
arr[min_ind]=temp;
}
}
/*OUTPUT
Enter Number of Element for an Array : 10
Enter Array Element : 23 45 20 65 7 11 97 12 5 10
Array Elements Are : 23 45 20 65 7 11 97 12 5 10
After Sorting Array Elements Are : 5 7 10 11 12 20 23 45 65 97
--------------------------------
Process exited after 22.21 seconds with return value 0
Press any key to continue . . .
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.