//CIS 610-251: Data Structures and Algo //Assignment #3 Question 2 //Recursion //Due Oct 7,99 #include #define SIZE 5 class IntArray { public: float * elem; float * result; IntArray(); float prod(int); float Avg(int); void Max(int); }; //init array IntArray::IntArray() { elem=new float[SIZE]; result=new float; *result=0.0; } //returns product of array elements from 0 to n. float IntArray::prod(int n) { if (n>SIZE-1) return -1; return (!n)? elem[0] : elem[n] * prod(n-1); } //Calc Average of elements 0 to n float IntArray::Avg(int n) { return (n==1)? (elem[0]+elem[1])/SIZE : (elem[n]/SIZE+Avg(n-1)); } //max of all elements in array void IntArray::Max(int n) { if (n *result) *result = elem[n]; Max(n+1); } } main() { IntArray arr; arr.elem[0]=1.0; arr.elem[1]=22.0; arr.elem[2]=10.0; arr.elem[3]=4.0; arr.elem[4]=5.0; cout << "\nProduct = " << arr.prod(4); cout << "\nAverage = " << arr.Avg(4); arr.Max(0); cout << "\nMax = " << *arr.result << '\n'; return 0; }