// https://wokwi.com/projects/359387026908041217
// https://forum.arduino.cc/t/obtaining-the-maximum-element-from-an-array/1102930
int Array [10]; //... you need to, at some point, make room for N elements
int i;
void setup() {
Serial.begin(9600);
}
//... the arguments can have any names you'd like
int maximum (int anArray[], int n) {
int max = 0;
Serial.println("in the maximum function:");
for (int i = 0; i < n; i++ ) {
if ( anArray[i] > max) {
max = anArray[i];
Serial.print(" The (new) max value is: ");
Serial.println(max);
}
}
//... you promised to return ann int, but failed to
return max;
}
void loop() {
for (int i = 0; i < 10; i++) {
//... statement does... absolutely nothing
// Array [i];
//... check random - the upper limit is not returned!
int num = random (0,10); //... random number 0..9 inclusive
Array [i] = {num};
Serial.print(Array[i]);
Serial.print(",");
delay(150); //... life to short!
}
//... I moved this print statement. Sue me.
Serial.println(" Full Array ");
//... this is a declaration. it does not execute any code
// int maximum (Array, 10);
//
// you want to actually call the function you wrote, and assign the returned value value.
int theMaximum = maximum(Array, 10);
Serial.print(" maximum from function call ");
Serial.println(theMaximum);
delay(300); //... life too short!
}