Trying to find the largest in an Array

Hello,

I'm trying to figure this thing out...I want to find the largest number in an array, but I also want to do it using a function/method in separate header and cpp files (i.e. class), so I can get some practice.

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
 #include <iostream>
#include <string>

#include "FindMax.h"

using namespace std;



int main()
{

    FindMax Obj;

    int[] x = new int[] {5454, 333, 922929, 22, 39, 0, 122, 92, 92122, 9};

    int y = Obj.findMax(x);

    cout << "Largest number in the array is " + y << endl;

    return 0;
}

//-----------------------------------------------------------------------------


#ifndef FINDMAX_H
#define FINDMAX_H


class FindMax
{
    public:

int findMax (int[] array){
int temp = array[0];
}
    private:

#endif // FINDMAX_H


//------------------------------------------------------------------------------

#include "FindMax.h"

FindMax::FindMax(int[] array)
{

    for (int i = 0; i < array.length; i++){
        if (array[i] > temp){
            temp = array[i];

        }}

    return temp

}

FindMax::~FindMax()
{
    //dtor
}

Last edited on
For one line 19 is supposed to be y not x. Where are you stuck?
Last edited on
ok, in the FindMax.cpp file, would you take another look at the loop:
an int array doesn't contain a member variable called length, you might consider adding another argument to this function that represents the array length, and using that argument to control the loop, or if you see another solution better, it's your call.
before the loop you should initialize temp to some member of the array.
i think you need some more understanding on how classes work, and how to use them effectively.
one more thing, a constructor can't have a return statement.
i think you really need some more practice on classes.
if i may, i want to correct this class for you, my friend:

FindMax.h:
1
2
3
4
5
6
7
8
9
#ifndef FINDMAX
#define FINDMAX

class FindMax
{
public:
    static int max(int [], int);
};
#endif 



FindMax.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "FindMax.h"
int FindMax::max( int [] a, unsigned int size)
{
    int temp = a[0];
    for(int i=0 ; i<size ; i++) //this will generate a warning,
    {                           //you can either ignore it, or correct it.
        if(a[i]>temp)
            temp = a[i];
    }
    return temp;
}


you can call the function like this:
FindMax::max(x,10); as an example.
It looka like you are mixing uo C++ with C#. There is no such a syntactic constrauction as

int[] x = new int[] {5454, 333, 922929, 22, 39, 0, 122, 92, 92122, 9};


in C++.
Last edited on
Yeah I do agree with you...I definitely need more practice on classes. It's embarrassing, but I'm trying to get better.

I understand how to use classes, methods, constructors, etc. pretty well in C#, but for some reason, C++ always trips me up with the separate header files and cpp files. That's why I'm here.



I don't think this has anything to do with your answer, but I got the following error:

fatal error: FindMax.h: No such file or directory
Last edited on
To find the maximum element in an array you can use standard algorithm std::max_elemnt. All what you need is to include header <algorithm> and maybe <iterator>
Topic archived. No new replies allowed.