0

I've got the following problem:

"Write a template function vectorMAXMIN() that will accept a vector and a number indicating the size of the vector and will return the max and the min values of the vector"...

So i think in it... Create a class vector to avoid the "size" passing value and control the insertions and can get from this the max and min values... ( dunno if it's a good idea )

The problem is "how to return a tuple?" When i read the problem, i thought in a tuple to return "max, min values" is it correct?

The code:

#include <iostream>

template < typename T >
class _tuple
{
    public:
        T _Max;
        T _Min;
};

template < typename T >
class _vector
{
    public:
        _vector( int cnt = 0);
        ~_vector();
        _tuple< T > get_tuple( void );
        void insert( const T );
    private:
        T *ptr;
        int cnt;
        int MAX;
};

template < typename T >
_vector< T >::_vector( int N )
{
    ptr = new T [N] ; 
    MAX = N;
    cnt = 0;
}

template < typename T >
_tuple<T> _vector< T >::get_tuple( void )
{
    _tuple< T > _mytuple;
    _mytuple._Max = ptr[0];
    _mytuple._Min = ptr[0];
    for( int i = 1; i < cnt; i++)
    {
        if( _mytuple._Max > ptr[i]  )
            _mytuple._Max = ptr[i];
        if( _mytuple._Min < ptr[i] )
            _mytuple._Min = ptr[i];
    }
    return _mytuple;
}   

template < typename T >
void _vector< T >::insert( const T element)
{
    if( cnt == MAX )
        std::cerr << "Error: Out of range!" << std::endl;
    else
    {
        ptr[cnt] = element;
        cnt++;
    }
}

template < typename T >
_vector< T >::~_vector()
{
    delete [] ptr;
}

int main()
{
    _vector< int > v;
    _tuple < int > t;
    v.insert(2);
    v.insert(1);
    v.insert(5);
    v.insert(0);
    v.insert(4);
    t = v.get_tuple();
    std::cout << "MAX:" << t._Max;
    std::cout << " MIN:" << t._Min;
    return 0;
}
James
  • 5,355
  • 2
  • 18
  • 30
fpointbin
  • 1,633
  • 3
  • 16
  • 20
  • 5
    If you only need two values, you can use `std::pair`. – James McNellis Mar 01 '11 at 23:21
  • in `get_tuple`, you should check the vector's not empty, and you've got min and max back the front: i.e. `if (max > ptr[i]) max = ptr[i];` reduces `max` to the smallest value. ge_tuple's a bad name too... get_min_and_max is better - what it does rather than the generic return type. – Tony Delroy Mar 02 '11 at 02:01
  • Oh! I forgot it! Thanks, ill add it! – fpointbin Mar 02 '11 at 02:05

4 Answers4

3

I would use a std::pair to return the minimum and maximum values.

Also, if you're doing this in C++, you might want to use the std::vector implementation instead of a homegrown one and "just" scan the passed-in std::vector once instead of implementing your own vector. Though, the way the question reads, you're probably expected to pass in an array and the array size instead.

Timo Geusch
  • 24,095
  • 5
  • 52
  • 70
  • Good explanation! Thank you! Im using C++, but im reading a book and i haven't read STL yet... But ill take your advices! – fpointbin Mar 01 '11 at 23:34
  • FYI, STL was absorbed into the C++ standard library long ago. It's no longer a separate entity like Boost is now. – Mike DeSimone Mar 01 '11 at 23:38
  • You should probably get a better book, which uses these basic infrastructure from the start then. I would recommend "Accelerated C++" or "Programming P&P using C++". See [this List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Fabio Fracassi Mar 02 '11 at 09:59
2

Simulation of tuples is not necessary in general with recent compilers, and at all for your particular use case. For your use case -- a tuple of two values -- std::pair is perfectly suitable (as James McNellis mentioned). For tuples of two or more values, TR1 provides std::tr1::tuple and C++0x provides std::tuple.

EDIT: For older compilers lacking TR1 or C++0x support, of course there is boost to come to the rescue -- see in particular boost.tuple and boost.fusion.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • Thanks i will try to take your advice when i read C++0x in my book! Im newbie now! I coded the problem using std::pair! – fpointbin Mar 02 '11 at 01:14
0

Or just use std::pair.

genpfault
  • 51,148
  • 11
  • 85
  • 139
0

@Timo Geusch... i was reading a bit more and i code it... do you think is it correct?:

#include <iostream>
#include <vector>
#include <utility>

using namespace std;

int main()
{
    int a[] = {1,6,5,-1};
    vector< int > v( a, a+4 );
    pair < int, int > _tuple( *v.begin(), *v.begin() ); // < MAX, MIN >
    for( vector< int >::iterator it = v.begin() + 1; it != v.end(); it++ )
    {
        if( _tuple.first > *it )
            _tuple.first = *it;
        if( _tuple.second < *it )
            _tuple.second = *it;
    }
    cout << "MAX:" << _tuple.first << endl;
    cout << "MIN:" << _tuple.second << endl;
    return 0;
}
fpointbin
  • 1,633
  • 3
  • 16
  • 20