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.
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:
#include "FindMax.h"
int FindMax::max( int [] a, unsignedint 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.
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:
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>