Checking ascending order of array
Hi got an array form main method which is entered by the user (array size not known until input by user) and then theres a method which returns a boolean of true or false if the array entered is in ascending order. I can get the user input fine, just having trouble with the logic of the method to check if the arrays in ascending order. Heres my code:
public static boolean numerical(int[] a){
boolean result = false;
for(int i=0;i<a.length-1;i++){
if(a[i] < a[i+1]){
result = true;
}
else if(a[i] > a[i]+1){
result = false;
}
if(result != true){
return result;
}
}
return result;
}
I thought this would work but I get not what I wanted when I run the code. I thought that we can check the array contents in order from i against the number nearest to it i+1 then if the current i in the array is > then the contents of i+1 it would return the boolean false, as it shows the arrays not in order. Could someone help with the logic of this or help with my code.
This is a homework exercise so I have to write the searching by myself without like already built array methods.
Thanks in advance