Homework-4
Due: Friday, Oct 11, 2008: I will give you an extra day
(instead of Oct 10). No late submission for this,
as the TA has to grade it.
Note: Please follow the following instructions when you submit
your HW4 solutions:
- You may use multiple files for each problem.
Please use proper names for each file.
(e.g., HW4_Pr1-a.c, HW4_Pr1-b.c, etc.).
- Make a zip file off all your files.
- Submit your zip file into "Course Files" &rarr "Shared Files" &rarr "HW3"
folder.
1. Write a sorting function using templates
that takes an array of any generic type as input and
prints the values in an ascending order.
In the main program, ask the user to enter integers.
Read the values until a negative number is encountered.
Sort them by calling the sorting function and print the values.
Next ask the user to enter doubles, sort them using the same function
and print the values.
You can assume that the user enters no more than 100 values
in both the cases.
2. Write a template class that uses variables of a generic type.
The class should store an array of the type and
should define the following public methods:
- Find the maximum value in the array, that returns a value
of the same type as those in the array.
- Find the minimum value in the array, that returns a value
of the same type as those in the array.
- Find the mean of all the values in the array,
that returns a double.
- In the main program ask the reader to enter integer values.
Read them until a negative number is encountered.
Store them in the array. Find the maximum value and minimum value
entered as well as their mean and print them all.
Repeat the same with doubles.
Note: You can assume that the user enters no more than 100 values
in both the cases and all of them are positive.
3. Let's define a simple class called myString as follows.
class myString
{
private:
char value[20]; // Assume no more than 20 characters for simplicity.
public:
myString (char *str) { strcpy (value, str); } // Constructor
void printString ( ) { cout << value << endl; }
myString& operator+= (const myString& s1);
};
In the above code, "+=" is a concatenation operator.
That is, let str1 and str2 be two objects of type myString,
such that str1 is "abc" and str2 is "def".
We can concatenate str2 to str1 using "+=" operator.
That is, str1 += str2 will yield "abcdef".
- Implement the += operator.
- In the main program ask the user to input two
values for str1 and str2.
Create two objects of myClass using those values.
Print the two strings.
Use += operator to concatenate str2 to str1
and print the value on the screen after concatenation.
Ramana Isukapalli
Last modified: Fri Oct 3 00:07:14 EDT 2008