out: Friday, May 2, 2025
due: Monday, May 5, 2025 by 10:00 am,
No late exercises accepted.
Goals: Learn to use the C++ STL container vector
and some algorithms associated with it,
create and use a function template, and learn how to check for errors
when reading data from an input stream.
Description:
Write a C++ program that prompts the user to type in 6 doubles,
reads those doubles from stdin
into a std::vector
of doubles, sorts the
vector using std::sort
, and then prints out the sorted vector.
To gain additional 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);
As well, 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.
Altogether, your program should match the following transcript as closely as you can:
bash$ g++ -Wall -g -std=c++17 -o ex11 ex11.cc bash$ ./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 bash$
Your code must:
g++ -Wall -g -std=c++17 -o ex11 ex11.cc
const
and so on must make us
smile rather than cry. (Hints: Google C++ Style Guide, cpplint
)You should submit your exercise using the Gradescope dropbox linked on the course resources web page.