Exercise Create sortArray2cpp In this part of the lab you wi
Solution
#include<iostream>
#include<stdlib.h>
using namespace std;
//To accept n numbers into an array
void accept(int arr[], int n)
{
cout<<\"\ Enter \"<<n<<\" items into array: \";
for(int x = 0; x < n; x++)
cin>>arr[x];
}//End of accept
//To display the array contents
void display(int arr[], int n)
{
cout<<\"\ Contents of array: \";
for(int x = 0; x < n; x++)
cout<<arr[x]<<\" \";
}//End of display
//Sort the array using Selection Sort in Ascending order with Minimum choice
//Returns the swap count
int sortAscendingMin(int arr[], int len)
{
//Initially swapCount is zero
int mini, loc, temp, x, y, swapCount = 0;
//Loops till length -1 times
for(x = 0; x <= len - 1; x++)
{
//Stores the first value of the array as minimum
mini = arr[x];
//Stores the location
loc = x;
//Loops next position of the x value to the length - 1
for(y = x + 1; y <= len -1; y++)
{
//if array y position value is less than the minimum then update the minimum by array y position value
//and location to y
if(arr[y] < mini)
{
mini = arr[y];
loc = y;
}//End of if
}//End of for loop
//Swapping
if(loc != x)
{
//Increase the counter for swapCount
swapCount++;
temp = arr[x];
arr[x] = arr[loc];
arr[loc] = temp;
}//End of if
}//End of for loop
//Return the swap counter
return swapCount;
}//End of function
//Sort the array using Selection Sort in Descending order with Maximum choice
//Returns the swap count
int sortDescendingMax(int arr[], int len)
{
//Initially swapCount is zero
int max, loc, temp, x, y, swapCount = 0;
//Loops next position of the x value to the length - 1
for(x = 0; x <= len - 1; x++)
{
//Stores the first value of the array as maximum
max = arr[x];
//Stores the location
loc = x;
//Loops next position of the x value to the length - 1
for(y = x + 1; y <= len -1; y++)
{
//if array y position value is greater than the maximum then update the maximum by array y position value
//and location to y
if(arr[y] > max)
{
max = arr[y];
loc = y;
}//End of if
}//End of for loop
//Swapping
if(loc != x)
{
//Increase the counter for swapCount
swapCount++;
temp = arr[x];
arr[x] = arr[loc];
arr[loc] = temp;
}//End of if
}//End of for loop
//Returns the swapCount
return swapCount;
}//End of function
//Main function
int main()
{
int n, ch, co;
//Loops till 0 is entered by the user as choice
do
{
cout<<\"\ 0) EXIT \ 1) Minimum \ 2) Maximum \ Enter your choice: \";
cin>>ch;
//If choice is zero exit
if(ch == 0)
exit(0);
//Accepts the size of array
cout<<\"\ Enter the size of array: \";
cin>>n;
//If size is zero display the error message and terminate
if(n <= 0)
{
cout<<\"\ Array size cannot be negative or zero\";
exit(0);
}
//Declares an array of size n
int arr[n];
//Accepts data
accept(arr, n);
//Checks if choice is 1
if(ch == 1)
{
co = sortAscendingMin(arr, n);
display(arr, n);
cout<<\"\ This is the sorted array in Ascending Order\";
cout<<\"\ The algorithm selected Minimum for traversing the array\";
cout<<\"\ It took \"<<co<<\" swaps to sort the array\";
}//End of if
//Otherwise choice is 2
else
{
co = sortDescendingMax(arr, n);
display(arr, n);
cout<<\"\ This is the sorted array in Descending Order\";
cout<<\"\ The algorithm selected Maximum for traversing the array\";
cout<<\"\ It took \"<<co<<\" swaps to sort the array\";
}//End of else
}while(1);
}//End of main
Sample run 1:
0) EXIT
1) Minimum
2) Maximum
Enter your choice: 1
Enter the size of array: 5
Enter 5 items into array: 12
45
6
1
78
Contents of array: 1 6 12 45 78
This is the sorted array in Ascending Order
The algorithm selected Minimum for traversing the array
It took 3 swaps to sort the array
0) EXIT
1) Minimum
2) Maximum
Enter your choice: 2
Enter the size of array: 10
Enter 10 items into array: 11
56
98
4
8
23
45
66
87
90
Contents of array: 98 90 87 66 56 45 23 11 8 4
This is the sorted array in Descending Order
The algorithm selected Maximum for traversing the array
It took 8 swaps to sort the array
0) EXIT
1) Minimum
2) Maximum
Enter your choice: 0
Process returned 0 (0x0) execution time : 46.024 s
Press any key to continue.
Sample run 2:
0) EXIT
1) Minimum
2) Maximum
Enter your choice: 1
Enter the size of array: 0
Array size cannot be negative or zero
Process returned 0 (0x0) execution time : 5.496 s
Press any key to continue.



