Feb 27, 2016 12:09 PM
(This post was last modified: Feb 27, 2016 12:13 PM by Matt AH.)
Having some trouble figuring out why this ain't working.
Making a method that takes in an integer array full of numbers and I'm supposed to return the smallest and largest number in the array.
Upon finding the min and max I'm supposed to return it in a new integer array.
My method:
A test case code (Using jUnit):
Making a method that takes in an integer array full of numbers and I'm supposed to return the smallest and largest number in the array.
Upon finding the min and max I'm supposed to return it in a new integer array.
My method:
Code
public static int[] minmax(int[] a) {
int[] minmaxA = new int[2];
int max = 0;
int min = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
}
if (a[i] < min) {
min = a[i];
}
}
minmaxA[0] = min;
minmaxA[1] = max;
return minmaxA;
}A test case code (Using jUnit):
Code
assertArrayEquals(new int[] { 1, 9 }, ArrayUtil.minmax(new int[] { 1, 2, 7, 9 }));

![[Image: DTc14yu.png]](https://i.imgur.com/DTc14yu.png)

