To get the minimum value in an array, I made the method minValue
public int minValue() {
int smallestVal = 0; //field
if (intArray.length == 0) { //if array is empty, return 0
return 0;
}
int a = intArray[0]; //field
for (int i : intArray) {
if (i > a) {
smallestVal = a;
}
else {
a = i;
}
}
return smallestVal; //returns the smallest value
}
Tested it in a main method with arr9 = { 1, 2, -1, 40, 1, 40, 0, 0, -3, 2, 2, -2, -5, 0, 1, -4, -5 }
and arr10 = { 4, 5, 5, 4, 1, 5, -3, 4, -1, -2, -2, -2, -2, -2, -2, 1, 4, 5, -5 }
For arr9, it returns -5 but for arr10 it returns -3 instead of -5. Is there something I need to change in my code?
