Starting from:
$35

$29

Computer Assignment 02 STL algorithms Solution

Similar to **assignment01**, you are to implement two search algorithms (*linear search* and *binary search*) in C++ on randomly generated integers stored in vectors. In this assignment, you will use routines from STL <algorithm to implement these algorithms.




The program is partially implemented. The source file `assignment02.cc` is included within this Git repository.




In this file, the main function is already implemented. You are required to implement the following functions: * `void genRndNums(vector <int& v, int seed)` This routine generates random integers in the range `[ LOW = 1, HIGH =200 ]` and puts them in vector `v`. Initializes the random number generator (`RNG`) by calling the function `srand()` with the seed value seed, and generates random integers by calling the function `rand()`. To use `srand` and `rand`, you need the header `<cstdlib`. The vector `v` is already allocated with space. Use vector takes a pointer to the search routine `p()`, and then it calls `p()` for each element of vector `searchVec` in vector `inputVec`. It computes the total number of successful searches and returns that value to the `main()` routine as an input argument to the print routine `printStat()`, which is used to print out the final statistics for a search algorithm.




* `void sortVector(vector <int& inputVec)` A sort algorithm to sort the elements of vector `inputVec` in ascending order. To implement this routine, simply call the `sort()` function from the STL.




* `void printStat(int totalSucCnt, int vec_size)` Prints the percent of successful searches as floating-point numbers on `stdout`, where `totalSucCnt` is the total number of successful comparisons and `vec_size` is the size of the test vector.




* `void print_vec(const vector <int& vec)` This routine displays the contents of vector `vec` on standard output, printing exactly `NO_ITEMS = 12` numbers on a single line, except perhaps the last line. The sorted numbers need to be properly aligned on the output. For each printed number, allocate `ITEM_W = 5` spaces on standard output. You can re-use the implementation of this routine from Assignment01, but remember to change the values of related constants.




**Programming Notes:** * Include any necessary headers and add necessary global constants. * You are not allowed to use any I/O functions from the C library, such as `scanf` or `printf`. Instead, use the I/O functions from the C++ library, such as `cin` or `cout`. * You need to use correct implementation of `print_vec` from the first assignment. Please seek help from the TAs if necessary.




* Execute the `srand()` function only once before generating the first random integer with the given seed value `SEED`. The `rand()` function generates a random integer in the range `[0, RAND_MAX]`, where the constant value `RAND_MAX` is the largest random integer returned by the `rand()` function and its value is system dependent. To normalize the return value to a value in the range `[LOW, HIGH]`:

```c++ rand() % (HIGH o assignment02.exe`. This will create the executable file `assignment02.exe`. To test your program, execute `./assignment02.exe & assignment02.out`, which will put the output (including any error messages) in file `assignment02.out`. You can find the correct output of this program in file `results02.out` included within this repository. * Add documentation to your source file (e.g. https://google.github.io/styleguide/cppguide.html#Comments). * Prepare your `Makefile` so that the TA only needs to invoke the command `make` to compile your source file and produce the executable file `assignment02.exe`. This semester you will need to make at least ***three commits*** to your local repository and ***three pushes*** to your master repository for each assignment. This will show the evolution of your assignments over time and the thought process behind the code. You will need to make sure your final ***push*** to your master repository is completed before the assignment is due (the system time stamps actions), if the assignment is late the TA will mark off points.




When your program is ready for grading, ***commit*** and ***push*** your local repository to remote git classroom repository and follow _**Assignment Submission Instructions**_.

More products