In this lab you'll get some experience creating basic functions and a class and turning them into templated versions.
How many times have you written a function that returns the smallest element in an array? Lots? Never? Well, either way, why don't we write a templated function that will find the minimum element in an array no matter what type we store in there!
Your job is to write a templated function minimum
that takes an array of elements and the number of elements in the array and returns the value of the smallest element in the array. It should work for doubles, strings, ints, etc. I suggest you write this as a regular old function for arrays of doubles first, then "templatize" it. Save it in a file called ArrayStuff.h
, and you should be able to compile and run main1.cpp.
Using the class defined in List.h from Class 13, write a templated function for printing the minimum element in a list. Its prototype should look like this:
template<class DataType>
DataType minimum(List<DataType> &L);
Once again, however, get it working for lists of some concrete type (string, double, char, whatever) and once it's working switch to templates! Remember not to presuppose a default minimum value since you don't know what you'll get! However, the first item in a list can indeed serve as an initial minimum! Save it in a file called ListStuff.h
; from there you should be able to compile and run main2.cpp.
Note: If you're using an
Iterator
and you run into an error that looks something like this:error: dependent-name 'List<DataType>::Iterator is parsed as a non-type, but instantiation yields a type note: say 'typename List<DataType>::Iterator' if a type is meantIt basically means that since
Iterator
is a nested class and since minimum isn't a member function, the compiler doesn't know thatIterator
is a data type until it is instantiated in main. This is related to our lecture discussion about splitting the interface and implementation, but even if we were to put this inList.h
we'd still get this error! If you do get this error, go ahead and addtypename
in front of your declaration in the manner it mentions. We won't likely discusstypename
again so you need not worry about the all specifics of why we need it.
Averager
for doublesForget about templates for a moment! Create a class Averager
implementing the following ADT:
void add2collection(double val); // add val to the Averager collection
double average(); // returns the average of the values in the Averager collection
Hopefully you see that this can be done pretty easily, without arrays or lists or anything! Put your code into a file named part3.h
and use main3.cpp to test your implementation!
Averager
Remember templates again! Create a templated class Averager
in a file named Averager.h
implementing the same ADT as in part 3, only this time the type stored in the collection is a template parameter. If DataType is the name of your template parameter, the ADT would look like this:
void add2collection(DataType val); // add val to the Averager collection
DataType average(); // returns the average of the values in the Averager collection
Modify main3.cpp as minimally appropriate to test your templated implementation with an Averager<double>
object and name your new file main4.cpp
.
In Parts 2-4 above, modify your solution so that it properly handles the user entering only a ';' without any other input. Warning: simply returning 0 is NOT an acceptable option.
Submit your source code via the the submisison website: submit.cs.usna.edu.
Specifically:
ArrayStuff.h
ListStuff.h
part3.h
Averager.h
and main4.cpp
.o
, or provided/unaltered files (i.e. main1.cpp, List.h, main2.cpp , or main3.cpp)!
~/bin/submit -c=SI221 -p=Lab08 ArrayStuff.h ListStuff.h part3.h Averager.h main4.cpp