Here is my code:
ArrayFunHouseTwo
public class ArrayFunHouseTwo
{
//goingUp() will return true if all numbers
//in numArray are in increasing order
//[1,2,6,9,23] returns true
//[9, 11, 13, 8] returns false
public static boolean goingUp(int[] numArray)
{
int num = 0;
int numMinus = 0;
for(int x = 1; x < numArray.length; x++){
System.out.println(x);
num = numArray[x];
numMinus = numArray[x-1];
if(num > numMinus){
return true;
}
}
return false;
}
and the associated runner class:
import java.util.Arrays;
public class Lab14b
{
public static void main( String args[] )
{
int[] one = {1,2,3,4,5,6,7,8,9,10};
int[] two = {1,2,3,9,11,20,30};
//add more test cases
//int[] three = {9,8,7,6,5,4,3,2,0,-2};
//int[] four = {3,6,9,12,15,18,21,23,19,17,15,13,11,10,9,6,3,2,1,0};
System.out.println(Arrays.toString(one));
System.out.println("is going Up ? " + ArrayFunHouseTwo.goingUp(one));
System.out.println(Arrays.toString(two));
System.out.println("is going Up ? " + ArrayFunHouseTwo.goingUp(two));
//add more test cases
//System.out.println(Arrays.toString(three));
//System.out.println("is going Up ? " + ArrayFunHouseTwo.goingUp(three));
//System.out.println(Arrays.toString(four));
//System.out.println("is going Up ? " + ArrayFunHouseTwo.goingUp(four));
I’m wanting it to print out and thus, prove that it is reading though the whole array, but the only array it’s reading through is the third one and it will print all the values at position x or the value of x, depending on how I code the println() statement
This means that as soon as you find a number greater than its previous number, it will terminate the loop. What you want to do at this point is something like this.
edit: fixed the condition to
<=basically you check consecutive numbers and if the numbers are equal or decreasing, you set a flag to false (indeed at this point you can return false)