This is the outline of the code. { try { vector<int> v; //code that gets input a
ID: 3623634 • Letter: T
Question
This is the outline of the code.{
try
{
vector<int> v;
//code that gets input and puts it into v
...
//code that prints each element of v
...
//a selection sort from the provided pseudo code
..
//code that prints each element of v
}
catch (out_of_range&)
{
cerr << "oops – some vector index out of range ";
}
catch (...) {
cerr << "oops – some exception ";
}
return 0;
}
psuedo code
// algorithm selectionSort( A : list of sortable items )
n = length(A)
for (k = 0; k < n-1; ++k) do:
index = k;
for (i = k+1; i < n; ++i) do:
if A[i] < A[index] then:
index = i
end if
end for
// swap two elements A[k] and A[index]
end for
// end algorithm
You should make a function prototype above main that defines what selection_sort’s return type
and parameters:
void selection_sort(vector<int> &v);
Using “void” in the front means that we will not return anything. Note that the “&” symbol in
front of v means that we are passing our parameter by reference.
Add your function below main, removing your selection sort code from main and placing it in the
body of selection_sort:
void selection_sort(vector<string> &v)
{
//Your sort code goes here
}
Now you need to call your function from main where your selection sort code used to be:
main()
{.
..
selection_sort(v);
...
}
2. Make your function throw an exception if it is passes an empty vector. Add an exception
class called “EmptyNotExpected”. Above main.
class EmptyNotExpected{};
This creates a class that you will use to differentiate this exception from other exceptions.
Next, you need to throw an exception in selection_sort if the vector that is passed to it does not
have any elements inside of it. To throw this exception, use:
throw EmptyNotExpected();
Run the program and do not input any thing into v to verify that your exception is thrown when v
is empty.
Now we want to differentiate this exception from other exceptions. You should have a two
catches right now. Add this after your try blog about the other exceptions. You must put
more specific exceptions before more specific exceptions to allow the exceptions to be caught
correctly: t
try
{
...
}
catch(const EmptyNotExpected &e)
{
cerr << "Nothing to sort.";
}
Run the program and do not input any thing into v to verify that your exception is thrown when v
is empty. Also test your program using typical input.
Explanation / Answer
Dear user, Here is the code below: 1. #include<iostream> #include<vector> #include<string> using namespace std; void selectionsort(vector<string> &v); void main( ) { vector<string> v; string s; int i,n; cout<<"Enter number of items"; cin>>n; cout<<"Enter list of sortable items:"; for(i=0;i<n;i++) {cin >> s; list.push_back(s); } selectionsort(v); cout<<"Strings After sorting:"; for( i = 0; i < n; i++ ) cout<<v[i]<<endl; system("pause"); } void selectionsort( vector<string> &v ) { string temp; int i,j; for( i = 0; i < v.size( )-1; i++) { for( j = i+1; j < v.size( );j++) { if( v[ i ].compare( v[ j ] ) > 0 ) { temp=v[i ]; v[i]=v[j]; v[ j] = temp; } } } } Note: One question per one post regarding cramster rules. Note: One question per one post regarding cramster rules.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.