Unfortunately Array's aren't my strong point and this final lab is giving me a bit of trouble. Basically I need to make it so the code will return true if the given array is sorted in ascending order (lowest to highest). or it will return false. Then i need to use from the main method to create and show two arrays and indicate whether or not they are sorted. I believe both my functions are correct however there's some logical issues going on I believe, I'm kinda stuck at this point, any help making since of this mess would be appreciated.

public static Random random = new Random(100);

    public static void main(String[] args) {
        int[] array1 = {5, 3, 6, 7, 0, 2, 4, 15, 1, 3};
        int[] array2 = {0, 1, 2, 3, 4, 6, 7, 15, 99, 100, 100};
        int[] array = createRandomArray(1000);


        System.out.println("Sorted: " + isSorted(array1)); // false
        System.out.println("Sorted: " + isSorted(array2)); // true

        System.out.println("DONE!");
    }

    // TODO: Return true if array is sorted, fals othrwise.
    // NOTE: should *NOT* use any of the methods of the "Arrays" Utility class!!!
    private static int[] createRandomArray(int length) {
		int[] result = new int[length];
		for(int i = 0; i < length; i++) {
			result[i] = random.nextInt();
		}
		return result;
	}
    
    
    
    static boolean isSorted(int[] theArray) {
        for(int i = 1; i < array.length; i++) {
                        if(array[i] < array[i-1]) {
                                return false;
                        }
                }
                return true;
        }
        boolean bArraySorted = true; // true until demonstrated false...
       
    }

Recommended Answers

All 8 Replies

You don't say what is your problem. The code seems ok. Does it compile? Does it run? Does it give correct results?
What are those logical issues?

Whoops, sorry bout that, the problem is that it's not compiling at all correctly, it's give my syntax errors for the print statements as well as

#
private static int[] createRandomArray(int length) {
#
int[] result = new int[length];
#
for(int i = 0; i < length; i++) {
#
result[i] = random.nextInt();
#
}
#
return result;

And? Don't you have anything else to post? The errors maybe. Do we have to chase you to tell us your problems/errors?

I'm simply asking for help suggestions not asking for anything of the sort, The reason I cannot post the errors is because I'm not at home to run it to compile them, it gave me errors for the two top print statements and then in the code I posted highlighting the word array in (below) as the error.

#
for(int i = 1; i < array.length; i++) {
#
if(array < array[i-1]) {

Read the error messages that you get. Have you defined the "array" variable? What does that method take as argument?

int[] array = createRandomArray(1000);

wouldn't that suffice for defining it was that setup in correctly? I wish I was home to actually run it to show the errors I know it was pin pointed to that isSorted function and for some reason the first print statement.

int[] array = createRandomArray(1000);

wouldn't that suffice for defining it was that setup in correctly? I wish I was home to actually run it to show the errors I know it was pin pointed to that isSorted function and for some reason the first print statement.

That is defind in the main method. I am talking about the isSorted method. Shouldn't you be looping the argument?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.