-
-
Save Lydon-01/5afb3a52952d9d3c5e3d949e5b342847 to your computer and use it in GitHub Desktop.
Find the smallest value in the parameter array, and find the largest value, and return the largest value minus the smallest value. If the array has length 0, return -1.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* find the biggest and smallest | |
*/ | |
public int findRange(int[] intArray) { | |
if(intArray.length == 0){ //check that the array is bigger than 0 | |
return -1; | |
} | |
int largest = intArray[0]; | |
int smallest = intArray[0]; | |
for (int x=1;x<intArray.length;x++){ // find the smallest | |
if (intArray[x] < smallest){ | |
smallest = intArray[x]; | |
} | |
} | |
for (int i = 1;i<intArray.length;i++){ // find the largest number | |
if (intArray[i] > largest){ | |
largest = intArray[i]; | |
} | |
} | |
return largest - smallest; //output the difference | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment