Created
January 31, 2014 16:57
-
-
Save JorgeOlvera/8736214 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//ARRAY: Given an array of ints of ODD length, | |
//look at the first, last, | |
//and middle values in the array and return the largest. The array length will be a least 1. | |
import java.util.Scanner; | |
public class oddArray{ | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
int places = input.nextInt(); | |
if (places%2 == 0){ | |
System.out.println("Please introduce an odd number"); | |
} | |
else{ | |
int [] myArray = new int [places]; | |
for (int i=0; i<myArray.length; i++) { | |
System.out.println("Enter array item " + i); | |
myArray[i] = input.nextInt(); | |
} | |
int largestnumber = checkFirstMidLast(myArray); | |
System.out.println("Largest number is:"); | |
System.out.println(largestnumber); | |
} | |
} | |
public static int checkFirstMidLast(int[] myArray) { | |
int a = myArray [0]; | |
int b = myArray[(myArray.length+1)/2-1]; | |
int c = myArray[myArray.length -1]; | |
int biggest = 0; | |
if (a > b && a>c){ | |
biggest = a; | |
} | |
else if (b>a && b>c ) { | |
biggest = b; | |
} | |
else if (c>b && c>a){ | |
biggest = c; | |
} | |
return biggest; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment