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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#ifndef ASSIGNMENTTHREE_H
#define ASSIGNMENTTHREE_H
template<class T>
T maximum(T first, T second)
{
if(first < second)
{
return second;
}
else
{
return first;
}
}
template<class T>
T minimum(T first, T second)
{
if(first < second)
{
return first;
}
else
{
return second;
}
}
template<class T>
T absolute(T value)
{
if(value < 0)
{
return value * -1;
}
else
{
return value;
}
}
template<class T>
T exponent(T base, int exponent)
{
if (exponent == 1)
{
return base;
}
else
{
return base * exponent(base, exponent - 1);
}
}
template<class T>
T sumElements(T array[], const int size)
{
if (size < 0)
{
return 0;
}
else
{
return array[size - 1] + sumElements(array[], size - 1);
}
}
|