Due: Saturday, July 20th, 2024 by 11:00 pm; 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 <class T> T ReadValue(istream& in);
As well, ReadValue
should detect conversion and unexpected EOF conditions,
and, in such cases, exit(EXIT_FAILURE)
after printing out a
helpful error message. 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$
As usual, your code must:
g++ -Wall -g -std=c++17 -o ex11 ex11.cc
-- but
don't forget to submit a Makefile
.Makefile
, as described above, that compiles the code
with the g++
options -Wall -g -std=c++17
cpplint
may be helpful in flagging potential style
problems. (You can ignore cpplint
complaints about
using namespace
directives)
You should submit your exercise to the course Gradescope.