CSE 333 Exercise 11

Out:   Friday, February 6
Due:   Wednesday, February 11 by 11 AM
Rating:   3 (note)
CSE 333 Exercise Rating Scale

Each exercise this quarter is rated on a integer scale of 1 – 5, inclusive, with 1 being the "least time-consuming" and 5 being the "most time-consuming".

This difficulty scale is meant as a rough guide for you in predicting the amount of time to set aside for each exercise as you balance the work required for 333 with your other obligations. However, it is necessarily imperfect as everyone's set of circumstances and experiences with the exercises differ. If your experience with an exercise does not align with its rating, that is not a reflection of you or your abilities.

Goals

  • Use the C++ STL container vector and some of its associated algorithms.
  • Implement error handling when reading data from an input stream.
  • Create and use a function template.

Problem Description

Create a C++ program that does the following:

  1. Prompts the user to type in 6 doubles.
  2. Reads those doubles from stdin into a std::vector of doubles.
  3. Sorts the vector using std::sort, and then prints out the sorted vector.

Altogether, your program should match the following transcript as closely as you can:

$ g++ -Wall -g -std=c++17 -o ex11 ex11.cc
$ ./ex11
Enter 6 doubles:
6.0
3.33
10.1
-10.5
6
1.441
Your sorted doubles are:
-10.5
1.441
3.33
6
6
10.1
$

Implementation Notes

Function Templates

To gain practice with templates, you should create and use a helper function called ReadValue to read each input value. This function should use templates to abstract away the type returned; i.e., it should have the following function prototype:

template <typename T> T ReadValue(istream & in);

Conversion Error and Handling

In addition, ReadValue should detect conversion errors and unexpected EOF conditions, and, in such cases, exit(EXIT_FAILURE) after printing out a helpful error message to cerr. Member functions of istream like .good can be useful for this. See the istream documentation.

Style Focus

Robustness

This exercise reads in user input via the command line. This can raise errors and needs to be checked accordingly. If unrecoverable errors are detected, be sure that you print a useful message to standard error and return EXIT_FAILURE from main.

Submission

Submit the following file(s) by creating an ex11-submit tag in your exercise repo before the assignment deadline. The file(s) should be located in the exact directory listed below, including capitalization:

  • ex11/ex11.cc

Your code must:

  • Compile without errors or warnings on CSE Linux machines (lab workstations, attu, or CSE home VM).
  • Have no runtime errors, memory leaks, or memory errors (g++ and valgrind).
  • Be contained in the file listed above that compiles with the command:
    $ g++ -Wall -g -std=c++17 -o ex11 ex11.cc
  • Have a comment at the top of your .cc file with your name(s) and CSE or UW email address(es).
  • Be pretty: the formatting, modularization, variable and function names, commenting, and so on should be consistent with class style guidelines. Additionally, the linter shouldn't have any complaints about your code (cpplint.py).
  • Be robust: your code should deal with hard-to-handle/edge cases and bogus user input (if there are any) gracefully.