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:
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:
  1. Find the maximum value in the array, that returns a value of the same type as those in the array.
  2. Find the minimum value in the array, that returns a value of the same type as those in the array.
  3. Find the mean of all the values in the array, that returns a double.
  4. 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. 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".
  1. Implement the += operator.
  2. 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