Created
May 3, 2010 16:24
-
-
Save iwhcoj/388275 to your computer and use it in GitHub Desktop.
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
//joanna wilbur | |
import java.util.*; | |
public class proj6q8 | |
{ | |
public static void main(String[]args) | |
{ | |
Scanner input = new Scanner(System.in); | |
int ArraySize; | |
System.out.println("How many integers are in the sequence?"); | |
ArraySize=input.nextInt(); | |
int [] UsersArray = new int[ArraySize]; | |
System.out.println("Enter an array and stop using -999."); | |
for (int i=0; i<ArraySize; i++) | |
{ | |
UsersArray [i] = input.nextInt(); | |
if (UsersArray [i]== -999) | |
break; | |
}//end for loop | |
if (ArraySize == UsersArray.length)// must subtract 1 because the flag -999 is also part of the array | |
// but not considered part of the sequence | |
{ | |
System.out.println("and is " + FindSubsequence(ArraySize, UsersArray) + " ints long"); | |
System.out.println(UsersArray.length); | |
} | |
else System.out.println("Array size entered is incorrect:" + UsersArray.length); | |
}//end main | |
public static int FindSubsequence(int ArraySize, int UsersArray []) | |
{ | |
int max=1, maxStart=0, maxEnd=0; | |
for (int x=0; x<ArraySize; x++)//outer loop goes through each int in the array | |
{ | |
for (int j=x+1;UsersArray[j-1]<UsersArray[j]; j++)//inner loop checks the x+1 int in the array, seeing if it increases | |
{ // and continues until the int preceding j is not smaller | |
if ((j-x)+1>(max)) //since the difference between j and x isn't inclusive, we must add 1. for ex: | |
//the sequence 1 3 4 6 9 would use array [j] or 9 - array [x] or 1 | |
{ // if the difference is larger than previous largest difference, then update | |
maxStart=x; | |
maxEnd=j; | |
max=(maxEnd-maxStart)+1; // again add 1 so it is inclusive. | |
} | |
} | |
} | |
System.out.println("The longest increasing contiguous sequence is "); | |
for (int i=maxStart; i<=maxEnd; i++) | |
System.out.println(UsersArray[i]); | |
return max; | |
}// end FindSubsequence | |
}//end class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if you put 1 1 1 it would result in an infinite loop. maxend would keep getting set to 0 so you'd get struck in the x loop.
comment that out. i just thought of other circumstances where that line forces an infinite loop. actually just delete it instead of commenting it out so you won't foget to delete it later.