3

I have this code in main:

Array<int> array1 = Array<int>(5);
array1.coutArray();
cout << "Minimal value: " << min(array1, 5);

And I need function to get minimal value of array. I Tried this function:

template<class Array>
Array min(const Array* data, int size) {
    T result = data[0];
    for (int i = 1; i < size; i++)
        if (result > data[i])
            result = data[i];
    return result;
}

But for every variant from internet I have one error about types of first argument. How to properly write this function or it's calling? Array class:

template <class T>
class Array {
protected:
    int size;
    T* DynamicArray;
public:
    Array() {};
    Array(size_t s) : size(s) {
        DynamicArray = new T[size];
        for (int i = 0; i < size; i++) {
            cout << "Element " << i+1 << ": ";
            cin >> DynamicArray[i];

        }
    }

    void coutArray() {
        for (int i = 0; i < size; i++) {
            cout << DynamicArray[i] << " ";
        }
    }

    ~Array() {
        delete[]DynamicArray;
    }
};

template<class Array>
Array getMin(Array* arr, int size)
{
    for (int i = 0; i < size; i++) {
        cout << arr[i];
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
vchkhr2
  • 45
  • 1
  • 6
  • What does your `main()` look like? – David G Mar 24 '20 at 21:28
  • Are you seriously? If I attach all the code, you write about minimal part. If only minimal part, you need more... Can you help me? What's wrong? – vchkhr2 Mar 24 '20 at 21:29
  • 1
    Try that: `Array min(const Array& data, int size)` – Dmitry Kuzminov Mar 24 '20 at 21:29
  • I have SO MANY questions about this code. Where did you get it? Why do you need it? (or think you need it?) – JohnFilleau Mar 24 '20 at 21:29
  • Note that your code, as shown, has other problems, but your IMMEDIATE issue can be solved by changing the function `min` to take an `Array&` (Array reference) instead of a pointer. – JohnFilleau Mar 24 '20 at 21:32
  • Where are you learning C++? There's a few issues going on that I think could be solved by getting an actual, physical, tutorial [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – JohnFilleau Mar 24 '20 at 21:35

2 Answers2

3

If you need this for your Array template, it would be best to provide begin and end for it:

template <class T>
class Array {
protected:
    int size;
    T* DynamicArray;
public:
    ....

    using value_type = T;

    const T* begin() const {
        return DynamicArray;
    }

    const T* end() const {
        return DynamicArray + size;
    }

    T* begin() {
        return DynamicArray;
    }

    T* end() {
        return DynamicArray + size;
    }
};

Then you should be able use STL algorithms.

template<typename T>
T minimum(const T &tab)
{

    return *std::minimum_element(std::begin(tab), std::end(tab));
}

Note that your array is poor version of std::vector.

Marek R
  • 32,568
  • 6
  • 55
  • 140
0

The function getMin should be either a member function of the class or a friend function of the class to have access to protected data members of the class.

Here is a demonstrative program that shows how the function can be defined as a member function of the class. I also made some minor changes in the class definition.

#include <iostream>

template <class T>
class Array {
protected:
    size_t size;
    T* DynamicArray;
public:
    Array() : size( 0 ), DynamicArray( nullptr ) {}

    Array(size_t s) : size(s) {
        DynamicArray = new T[size];
        for ( size_t i = 0; i < size; i++) {
            std::cout << "Element " << i+1 << ": ";
            std::cin >> DynamicArray[i];

        }
    }

    void coutArray() const {
        for ( size_t i = 0; i < size; i++) {
            std::cout << DynamicArray[i] << " ";
        }
    }

    ~Array() {
        delete[]DynamicArray;
    }

    const T * getMin() const 
    {
        T *min = DynamicArray;

        for ( size_t i = 1; i < size; i++ ) 
        {
            if ( DynamicArray[i] < *min ) min = DynamicArray + i;
        }

        return min;
    }
};

int main() 
{
    Array<int> a( 5 );

    const int *min = a.getMin();

    if ( min != nullptr ) std::cout << "The minimum is equal to " << *min << '\n';

    return 0;
}

The program output might look for example the following way

Element 1: 2
Element 2: 3
Element 3: 1
Element 4: 4
Element 5: 5
The minimum is equal to 1

And below there is a demonstrative program when the function is defined as a non-template friend function of the class.

#include <iostream>

template <class T>
class Array {
protected:
    size_t size;
    T* DynamicArray;
public:
    Array() : size( 0 ), DynamicArray( nullptr ) {}

    Array(size_t s) : size(s) {
        DynamicArray = new T[size];
        for ( size_t i = 0; i < size; i++) {
            std::cout << "Element " << i+1 << ": ";
            std::cin >> DynamicArray[i];

        }
    }

    void coutArray() const {
        for ( size_t i = 0; i < size; i++) {
            std::cout << DynamicArray[i] << " ";
        }
    }

    ~Array() {
        delete[]DynamicArray;
    }

    friend const T * getMin( const Array &a ) 
    {
        T *min = a.DynamicArray;

        for ( size_t i = 1; i < a.size; i++ ) 
        {
            if ( a.DynamicArray[i] < *min ) min = a.DynamicArray + i;
        }

        return min;
    }
};

int main() 
{
    Array<int> a( 5 );

    const int *min = getMin( a );

    if ( min != nullptr ) std::cout << "The minimum is equal to " << *min << '\n';

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335