Originally Posted by
disturbed14x
i'm still a little confused i can understand for the most part how to do the max function, but i keep messing with it because i need to have a separate function called main and i need to call the function in order to solve the max problem for the array in the main functions.
Well, if that's the case. You need to pass the array[] and length variable from main() to the function by parameter.
Code:
#include <iostream>>
using namespace std;
int findMax(int array[], int length);
int main() {
int array[10] = { 3, 5, 2, 7, 8, 4, 6, 7, 4, 11 };
int length = (sizeof(array) / sizeof(int)); // Getting the size of the array
int maxNum = findMax(array, length); // Since the function returns a variable, you need something to hold the variable
// Passed the array[] and length variable as argument to the function
cout << maxNum << endl; // Display the max value returned
return 0;
}
int findMax(int array[], int length) { // Pass an array and a length variable as parameter
int max = 0;
for (int i = 0; i < length; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
Bai