### Help with homework, sorry im a Noob

Hi guys,

I am taking a Begginers C++ course, and so far i have been able to grasp everything, but then he jumped into templates and arrays, here is what he assigned

Create a header file mymath.h. This header file will contain math function that will work with any data type. In this header file, create the following functions:

· T maximum(T first, T second): This function returns the maximum of first and second.

· T minimum(T first, T second): This function returns the minimum of first and second.

· T absolute(T value): This function returns the absolute value of the argument value.

· T exponent(T base, int exponent): This function returns the first argument, base, raised to the second argument, exponent. This function must be recursive.

· T sumElements(T array[], const int size) : This function will return the sum of the elements in the array. This function must be recursive.

· T findMinimum(T array[], const int size) : This function will return minimum element in the array. This function must be recursive.

· T findMaximum(T array[], const int size) : This function will return maximum element in the array. This function must be recursive.

here is what i have on my header, any clues or help are welcomed

Thanks a Bunch

A noob

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667`` `````` #ifndef ASSIGNMENTTHREE_H #define ASSIGNMENTTHREE_H template T maximum(T first, T second) { if(first < second) { return second; } else { return first; } } template T minimum(T first, T second) { if(first < second) { return first; } else { return second; } } template T absolute(T value) { if(value < 0) { return value * -1; } else { return value; } } template T exponent(T base, int exponent) { if (exponent == 1) { return base; } else { return base * exponent(base, exponent - 1); } } template T sumElements(T array[], const int size) { if (size < 0) { return 0; } else { return array[size - 1] + sumElements(array[], size - 1); } }``````
sumElements() doesn't work.
When size = 0 it will try to access array[-1] resulting in undefined behavior.
Returning 0 is not a good idea since it will only work if T is convertible to the type of 0 (should be int, but I'm not sure).
To call the function the correct syntax is `sumElements(array, size); // not array[] `

The rest seems ok to me.
Last edited on
ok i fixed it, but now im stuck on the next part, how would i write in the main to access it or in other words make it work?? would this work

 ``12345678`` ``````void testArraySum() { const int size = 3; int array[] = {1,1,1}; sumElements(array, size); cout << array[] << endl; }``````
 ``123456789`` ``````void testArraySum() { const int size = 3; int array[] = {1,1,1}; // You should be able to just call sumElements(array, size) // The template type will be deduced from the type of the first argument cout << sumElements(array, size) << endl; }``````
Last edited on
You guys rock, but after trying all night I cant seem to figure out the minimum and maximum arrays, would you guys take a look at what I got see what I am doing wrong plz.

Thank You Again

 ``1234567891011121314151617181920`` ``````template T findMinimum(T array[], const int size, int min) { if (min == 1) { return array[0]; } else { min = findMinimum(array + 1, size - 1, 3); if(min < array[0]) { return min; } else { return array[0]; } } }``````
Topic archived. No new replies allowed.