Question

All functions need to work. For sort function need to test 1) Test selection sort for...

All functions need to work. For sort function need to test

1) Test selection sort for vector of integers.

2) Test selection_sort for vector of doubles.

3)Test selection_sort for vector of strings.

4) Test getElement function for vector of ints.

In part one, you are going to implement the selection sort function that is capable of sorting vectors of int, double or string. In part two you will be writing a try catch block to catch the out-of-range exception.

You are to write three functions and manipulate main() function for this lab all of which should be written in one main.cpp file:

Part one:

unsigned min_index(const vector<T> &vals, unsigned index): Passes in an index of type int and a vector of type T (T could be string, double or int). The function returns the index of the min value starting from "index" to the end of the "vector".

selection_sort(vector<T> &vals): Passes in a vector of type T and sorts them based on the selection sort method. This function should utilize the min_index function to find the index of the min value in the unsorted portion of the vector.

Part two:

T getElement(vector<T> vals, int index): Passes in a vector of type T (T could be string, double or int) and an index of type int. The function returns the element located at the index of the vals. You should write up a try catch block in main function so that when the index is out of the range of the vector, the "std::outofrange" exception will be caught by the catch (const std::outofrange& excpt). Once the exception is caught, it should output "out of range exception occured" followed by a new line.

You are given a function that creates a vector of characters with random size. You just need to put the following piece of code in your main.cpp file as is:

vector<char> createVector(){
    int vecSize = rand() % 26;
    char c = 'a';
    vector<char> vals;
    for(int i = 0; i < vecSize; i++)
    {
        vals.push_back(c);
        c++;
    }
    return vals;
}

Here is the main function you can use. You should add try/catch block to this function so that if index is out of range in calling getElement function, it should output "out of range exception occured" followed by a new line. You should include <stdexcept> library in your program which contains definitions for a set of standard exceptions.

int main(){

    //Part B
     srand(time(0));
     vector<char> vals = createVector();
     char curChar;
     int index;
     int numOfRuns = 10;
     while(--numOfRuns >= 0){
         cout << "Enter a number: " << endl;
         cin >> index;
         curChar = getElement(vals,index);
         cout << "Element located at " << index << ": is " << curChar << endl;
    }
    return 0;
}

You should come up with test harnesses to test your selection_sort function.

In this lab, you are going to write a template function that is capable of sorting vectors of characters, intgeres and strings.

LAB

0 0
Add a comment Improve this question Transcribed image text
Answer #1

main.cpp

#include <iostream>
#include<stdlib.h>
#include<vector>
#include<time.h>
#include <stdexcept>

using namespace std;

// swap element x and y
void swap(int &x, int &y)
{
int temp = x;
x = y;
y=temp;
}
// returns the index of the min value starting from "index" to the end of the "vector" vals.
template <typename T>
unsigned min_index(const vector<T> &vals, unsigned index)
{

unsigned min_idx=index;
for (int j = index+1; j < vals.size(); j++)
if (vals[j] < vals[min_idx])
min_idx = j;
return min_idx;
}

//Passes in a vector of type T and sorts them based on the selection sort method.
template <typename T>
void selection_sort(vector<T> &vals)
{
int size=vals.size();
int i, j, min_idx;
// One by one move boundary of unsorted sub array
for (i = 0; i < size-1; i++)
{
// Find the minimum element in unsorted array
min_idx = min_index(vals,i);
// Swap the found minimum element with the first element
swap(vals[min_idx], vals[i]);
}
}

template <typename T>
T getElement(vector<T> vals, int index)
{
return vals.at(index);
}

vector<char> createVector(){
int vecSize = rand() % 26;
char c = 'a';
vector<char> vals;
for(int i = 0; i < vecSize; i++)
{
vals.push_back(c);
c++;
}
return vals;
}//create vector

int main()
{

srand(time(0));
vector<char> vals = createVector();
//cout<<"size of vector ="<<vals.size()<<" ";
char curChar;
int index;
int numOfRuns = 10;
while(--numOfRuns >= 0){
cout << "Enter a number: " << endl;
cin >> index;
/*
add try/catch block to this function so that if index
is out of range in calling getElement function, it should
output "out of range exception occured" followed by a new line
*/
try{
curChar = getElement(vals,index);
}//try
catch(const out_of_range excpt){
std::cerr << "Out of Range error: " << excpt.what() << ' ';
}//catch
cout << "Element located at " << index << ": is " << curChar << endl;
}//while

return 0;
}//main

//output

// --------------thank you----------------------

Add a comment
Know the answer?
Add Answer to:
All functions need to work. For sort function need to test 1) Test selection sort for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT