### 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.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465`` `````` #include #include #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:
 ``123456789`` ``````#ifndef FINDMAX #define FINDMAX class FindMax { public: static int max(int [], int); }; #endif ``````

FindMax.cpp
 ``1234567891011`` ``````#include "FindMax.h" int FindMax::max( int [] a, unsigned int size) { int temp = a[0]; for(int i=0 ; itemp) 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.