Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Let the user choose ascending or descending order by answering the question. The

ID: 3630352 • Letter: L

Question

Let the user choose ascending or descending order by answering the question. Then you must save this order flag and pass it to a revised sorting function that can be defined like this:


void sort2(int a[], int number_used, bool ascending);

Based on this ascending true or false, you make the different comparisons to fit ascending or descending order. Just make use of either fill_array or fill_array_Rand to initialize your test data. A sample program can be run like this:
=========================================================================

An enhanced version of Selection Sort: Do you want an ascending order? (y/n) y
Fill out 20 Random numbers:
32 78 16 22 77 23 55 80 23 43 12 37 52 32 76 1 76 81 2 20
In sorted order the numbers are:
1 2 12 16 20 22 23 23 32 32 37 43 52 55 76 76 77 78 80 81
Try Again? (y/n) y

An enhanced version of Selection Sort: Do you want an ascending order? (y/n) n
Fill out 20 Random numbers:
54 81 62 25 43 11 74 57 32 75 77 46 84 84 82 95 94 86 49 85
In sorted order the numbers are:
95 94 86 85 84 84 82 81 77 75 74 62 57 54 49 46 43 32 25 11
Try Again? (y/n) n

Explanation / Answer

please rate - thanks

#include<iostream>
using namespace std;
void fill_array(int a[],int number_used)
{int i;
cout<<"Fill out "<<number_used<<" Random numbers: ";
for(i=0;i<number_used;i++)
    cin>>a[i];
}
void selectionsort(int a[], int n,bool ascend)
{ int i,j,min,index,temp,k;
    for(i=0;i<n-1; i++)
    {index=i;
    min=a[i];
      for(j=i+1;j<n;j++)
         {if(ascend)
            {
             if(min>a[j])  
                 {index=j;
                  min=a[j];
                  }
            }
       else
            {
             if(min<a[j])  
                 {index=j;
                  min=a[j];
                  }
            }
             
    }
    temp=a[i];
    a[i]=a[index];
    a[index]=temp;
            
    }

}


int main()
{int a[20];
int number_used=20,i;
char y;
bool ascend;
do
{
cout<<"An enhanced version of Selection Sort: Do you want an ascending order? (y/n) ";
cin>>y;
if(y=='y')
     ascend=true;
else
     ascend=false;
fill_array(a,number_used);
cout<<"In sorted order the numbers are: ";
selectionsort(a,number_used,ascend);
for(i=0;i<number_used;i++)
    cout<<a[i]<<" ";
cout<<endl;
cout<<"Try Again? (y/n) ";
cin>>y;
}while(y=='y');                        
return 0;
}