In: Computer Science
*Answer must be in C++ and please use a Windows machine NOT IOS!
Write a program as follows:
Ask the user for an integer representing a number of integers to be sorted.
Create an array of integers of the size provided by user.
Initialize the array to zeros.
Ask the user for and populate the array with user input.
Output the array elements on one line separated by a space.
Write a function name “supersort” that takes an integer pointer for the starting memory address of an array and an integer variable representing the number of elements in that array. The function should sort the array elements from low to hi when called.
Output the array elements again in main after the function supersort is called.
source code of the above question
#include<iostream.h>
void supersort(int*,int); //function prototype
// Driver code
int main()
{
int n,i,Arr[20],*Ptr;
cout<<"enter the number of integers to be
sorted";
cin>>n;
for(i=0;i<n;i++)
Arr[i]=0; // initializing the array to 0;
cout<<"enter the elements into array" ;
for (i=0;i<n;i++)
cin>>Arr[i];
//entering intigers to array
Ptr=Arr;
supersort(Ptr,n);
//function calling using pointer to array
// print the elementss of array
cout<<"the array elements after sorting
are"<<endl;
for (i = 0; i < n; i++)
cout<<" "<<Arr[i];
return 0;
}
// sorting the numbers using pointers in a userdefined
function
void supersort( int* p,int n)
{
int i, j, t;
// Sort the numbers using pointers
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (*(p+j) < *(p+i))
{
t = *(p+i);
*(p+i) = *(p+j);
*(p+j) = t;
}
}
}
}
screen shot of the above program given below
result after the execution of the program given below
(press alt+f5 to see the output of the program in turbo c++ compiler after running the program)