Question: Implementing Merge Sort with Vectors and Exception Handling Introduction to Merge Sort: Merge Sort is a divide - and - conquer sorting algorithm that divides

Implementing Merge Sort with Vectors and Exception Handling
Introduction to Merge Sort:
Merge Sort is a divide-and-conquer sorting algorithm that divides an array (or vector) into two
halves, recursively sorts each half, and then merges the sorted halves to produce a fully sorted
array. This algorithm is efficient for large datasets.
In this exercise, you will implement the Merge Sort algorithm while working with vectors and
handling exceptions. The exercise includes several functions to manage vector operations and
the implementation of Merge Sort for sorting a vector of integers.
Your Tasks:
1) Create a function populateVector(std::vector& vec, int n):
This function should take a reference to a std::vector and an integer n.
Populate the vector with n random integers between 1 and 100.
2) Create a function printVector(const std::vector& vec):
Takes a constant reference to a std::vector.
Prints its elements.
3) Create a function int getElement(const std::vector& vec, int index):
Tries to access the element at the specified index in the vector.
Throws an std::out_of_range exception with a suitable error message if the index is out
of bounds.
4) Write a function mergeSort(std::vector& vec, char direction):
Implement the Merge Sort method to sort the vector in the specified direction ('a' for
ascending, 'd' for descending). Use the at() function to access elements and include try-
catch blocks to handle exceptions. Then, display the result.
5) In your main() function:
a) Read numbers from a file, ask the user how they would like to sort the numbers (in
ascending or descending order), sort them, and display the result.
b) Prompt the user to enter the size for creating a new vector and display it.
c) Ask the user to enter an index to retrieve an element value from this new vector. Use
try-catch blocks to handle any potential exceptions thrown when accessing the vector
elemen

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!