Using Class/Method to Find the Max Value of Array

I think I'm almost there...I wanted to create a class that had a member function that would be able to take into it's parameter any array of any length, and would give the maximum value...There are plenty of examples of how to find the max value of an array, but I had a hard time finding one that used a "general purpose" class/method to find it for an array that is inputted.

What I have with my logic, is that I'm asking for a number that is bigger than my first element. When I did the same thing in C#, it went all the way through the array, but for me right now, it stops whenever it reaches a number that is larger than it (I get 343 returned to me by the program), and does not go the entire way through...Thanks in advance.

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

#ifndef FindMaximum_h
#define FindMaximum_h

#include <iostream>
#include <string>

using namespace std;

class FindMax{

    public:
        
    int maxNum(int array[]){

    int temp = array[0];

    for (int i = 0; i < sizeof(array); i++) {

    if (array[i] > temp){
        temp = array[i];
    }
    }

    return temp;

    }
};

#include <iostream>
#include <string>
#include "FindMaximum.h"

using namespace std;

int main()
{

FindMax FindMaxArray;

cout << "Program to determine maximum number in Array." << endl << endl;

    int a[]={66, 9, 1, 343, 2, 44, 67, 3, 111, 4, 443, 5, 52, 6};

    cout << "The largest value is: " << FindMaxArray.maxNum(a) << endl;

    return 0;
}

Last edited on
I do not see any sense to create a class for the sake of one function.
Now about your errors. The declaration

int maxNum(int array[]);

is equivalent to declaration

int maxNum(int *array);

sizeof( array ) inside the function body that is equivalent to sizeof( int * ) usually equal to either 4 or 8 bytes.

So this loop

for (int i = 0; i < sizeof(array); i++)

is invalid. It would be also invalid if the function would deal with the array itself because number of elements for an array is calculated as sizeof( array ) / sizeof( *array ).

There is standard algorithm std::max_element in C++ that allows to get the element with the maximum value.
Last edited on
Hi Vlad,

I agree with you, but the reason why I created the class is because I need practice with C++ basics (i.e. classes, methods, separating main files from .h, .cpp, OO in general, etc.).

Thanks for your input.
In this case you have a choice either to declare the method as

int maxNum( int array[] , size_t n ) const;

or as

template <size_t N>
int maxNum( int ( &array )[N]) const;

Also you may declare the method as static.

I would name the class as something FindMaxInt and the method declare as the function call operator.
Last edited on
I agree with you, but the reason why I created the class is because I need practice with C++ basics


Creating a class like this shows a basic misunderstanding of what classes are. If a member function of a class doesn't access any other member of the class, then it shouldn't be a member function.
You will still occasionally come across utility classes, even though the idiomatic C++ approach is a namespace. A utility class is usually a set of static methods with some common theme.

Andy

Utility class
http://en.wikipedia.org/wiki/Utility_class
Creating a class like this shows a basic misunderstanding of what classes are. If a member function of a class doesn't access any other member of the class, then it shouldn't be a member function.


That's fine, no need to be insulting to a beginner. That's why I'm in this forum. I'm progressing faster with Java/C#, but have too much respect for C++ to not learn it, even if it is more difficult.

Thanks for the input.
Last edited on
In this case you have a choice either to declare the method as

int maxNum( int array[] , size_t n ) const;

or as

template <size_t N>
int maxNum( int ( &array )[N]) const;

Also you may declare the method as static.

I would name the class as something FindMaxInt and the method declare as the function call operator.


Thanks for the help, man!
That's fine, no need to be insulting to a beginner.

It wasn't an insult. It was an observation. An observation I would want made if I were the one presenting such code. Practicing stuff that reinforces a misconception is not... a good practice.
Wouldn't your conditional statement for the for-loop be i < sizeof(array)/sizeof(int) not i < sizeof(array). This is because sizeof returns the length in bytes not the number of elements. If your code however works without this, ignore what I just said but keep in mind that sizeof calculates bytes not number of elements

http://www.tutorialspoint.com/cplusplus/cpp_sizeof_operator.htm
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
class FindMaxInt
{
public:
     
    int * operator ()( int a[], size_t n ) const 
    {
        int *max = a;

        if ( max != a + n )
        {
            for ( int *p = a; ++p != a + n; )        
            {
                if ( *max < *p ) max = p;
            }
        }

        return max;
    }

    const int * operator ()( const int a[], size_t n ) const 
    {
        const int *max = a;

        if ( max != a + n )
        {
            for ( const int *p = a; ++p != a + n; )        
            {
                if ( *max < *p ) max = p;
            }
        }

        return max;
    }
};
By the way...

There are some OO purists who would prefer to use a class to represent the max (or even "be" the max), as opposed to a max finder. If you take this approach you end up with:

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
#ifndef MaxValue_h
#define MaxValue_h

#include <ostream>
// not good practice to use "using namespace std;"
// in a header file

class MaxValue{
private:
    int m_max;

public:
    MaxValue(int val = 0) : m_max(val){
    }

    MaxValue(const int a[], size_t n): m_max(0) {
        if(0 == n)
            return;
        m_max = a[0];
        for (size_t i = 1; i < n; ++i){
            if (a[i] > m_max){
                m_max = a[i];
            }
        }
    }

    MaxValue& operator=(int val) {
        m_max = val;
    }

    operator int() const {
        return m_max;
    }
};

// Edit: This isn't needed
//
// As vlad from moscow pointed out (below), there is no need for operator<<
// as the int case allows the compiler to use the int overload of operator<<
// to output the value of MaxValue variables.
//
//std::ostream& operator<<(std::ostream& os, const MaxValue& val) {
//    os << (int)val;
//    return os;
//}

#endif // MaxValue_h 


and

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include "MaxValue.h"

// simple macro for demo purposes
#define COUNTOF(a) (sizeof(a) / sizeof(a[0]))

using namespace std;

int main(){
    cout << "Program to determine maximum number in Array." << endl << endl;

    int a[]={66, 9, 1, 343, 2, 44, 67, 3, 111, 4, 443, 5, 52, 6};

    cout << "The largest value is: " << MaxValue(a, COUNTOF(a)) << endl;

    MaxValue max_of_a(a, COUNTOF(a));

    cout << "max_of_a = " << max_of_a << endl;

    return 0;
}


The int constructor, assignment operator, and cast operator allow MaxValue instances to take part in maths, etc. For example:

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
#include <iostream>
#include <limits>
#include "MaxValue.h"

// simple macro for demo purposes
#define COUNTOF(a) (sizeof(a) / sizeof(a[0]))

using namespace std;

int main(){
    const int a[] = { 66, 9,   1, 343, 2, 44};
    const int b[] = { 67, 3, 111,   4};
    const int c[] = {443, 5,  52,   6};
    const int d[] = {0};

    MaxValue max_of_a(a, COUNTOF(a));
    MaxValue max_of_b(b, COUNTOF(b));
    MaxValue max_of_c(c, COUNTOF(c));
    MaxValue max_of_d(d, 0);
    MaxValue max_zero;

    cout << "max_of_a = " << max_of_a << endl;
    cout << "max_of_b = " << max_of_b << endl;
    cout << "max_of_c = " << max_of_c << endl;
    cout << "max_of_d = " << max_of_d << endl;
    cout << "max_zero = " << max_zero << endl;
    cout << endl;

    cout << "2 x max_of_a  = " << 2 * MaxValue(a, COUNTOF(a)) << endl;
    cout << endl;

    cout << "sum of max values (a, b, c)  = "
         << max_of_a + max_of_b + max_of_c << endl;
    int mean = (max_of_a + max_of_b + max_of_c) / 3;
    cout << "mean of max values (a, b, c) = " << mean << endl;
    cout << endl;

    MaxValue min_of_max = numeric_limits<int>::max();
    if (max_of_a < min_of_max)
        min_of_max = max_of_a;
    if (max_of_b < min_of_max)
        min_of_max = max_of_b;
    if (max_of_c < min_of_max)
        min_of_max = max_of_c;
    cout << "min of max values (a, b, c) = " << min_of_max << endl;
    cout << endl;

    return 0;
}


Andy

PS For more on this view point, see (e.g.) "Object-Oriented Programming" by Coad and Nicola. The first example in this book is a class which represents a count; it's named count and not counter (after some debate.)
Last edited on
@andywestken


There is no any need to define this operator.

1
2
3
4
std::ostream& operator<<(std::ostream& os, const MaxValue& val) {
    os << (int)val;
    return os;
}


because there is a conversion function in your class.
As Vlad pointed out, you should be using things like std::max_element if you really want to understand the basics of C++. Reinventing the wheel is not a particularly good way of learning a computer language. You should be learning how to use the facilities of the language, and creating user defined types and algorithms only when it is necessary to do so.

http://www.cplusplus.com/reference/algorithm/max_element/
You should also have a look at these links.
http://www.cplusplus.com/reference/numeric/
http://www.cplusplus.com/reference/algorithm/count/

I can't understand why you'd want to invent a macro instead of just using a std algorithm that is part of the language specification.
There is no any need to define [operator<<].

Thanks, I've amended my code.

Got a bit too carried away!

Andy
Topic archived. No new replies allowed.