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

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);
    }
}
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

1
2
3
4
5
6
7
8
void testArraySum()
{
    const int size = 3;
    int array[] = {1,1,1};
    
    sumElements<int>(array, size);
    cout << array[] << endl;
}
1
2
3
4
5
6
7
8
9
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
template<class T>
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.