![]() |
|
[Java] Find Min/Max Values in an array, then return the min/max values in a new array - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] Find Min/Max Values in an array, then return the min/max values in a new array (/showthread.php?tid=5185565) |
Find Min/Max Values in an array, then return the min/max values in a new array - Matt AH - Feb 27, 2016 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: Code public static int[] minmax(int[] a) {A test case code (Using jUnit): Code assertArrayEquals(new int[] { 1, 9 }, ArrayUtil.minmax(new int[] { 1, 2, 7, 9 }));* - InfectCode - Feb 27, 2016 Your code is working, your test case is incorrect because you are passing an array from 1-9. When you are testing for min value 1 is not less than 0, so the min value would stay as 0. * - +Haze - Feb 27, 2016 Try this. Code public static int[] minMax(int[] numbers) {* - Matt AH - Feb 27, 2016 (Feb 27, 2016 12:33 PM)+Haze Wrote: Try this.god bless your soul nigga * - keram007 - Feb 29, 2016 Int[] intarr = new Int[10]; intarr[0] = 10; intarr[1] = 1; int imax = 0; for( int i ; intarr ) if( i > imax ) imax = i; * - kamika$e - Feb 29, 2016 The reason this method isn't working properly is that you have set the min and max equal to zero. So you are basically comparing if each number in the array is greater than or less than zero which doesn't make any sense whenever you are trying to find the min and max, right? I'm not going to take the time to compile and run this, but i believe your max would be the last integer in your array because 9 IS greater than zero and the min will be zero because none of the integers in your array is less than zero. The solution to this problem is to set the min and max to the first integer of the array. |