Sorting an array in ascending order
I want to do a code where: -Only accepts up to 50 integers -Sentinel value of 0 (zero) -Only accepts positive numbers (And ignores negative numbers) -Sorts the inputted numbers into ascending order. I use eclipse as my compiler. but when I run my code nothing appears. This is my code: https://code.sololearn.com/cxhIFj1f4J6Z
9/19/2021 10:24:32 AMEka
11 AnswersNew Answer
AJ The code is supposed to accept positive integers, to a maximum of 50, and use 0 as the sentinel value. It is not supposed to require the number of elements as the first entry. Entering 3 6 5 4 3 2 1 0 will throw an exception. Entering 3 6 5 4 will throw an exception. Your code also accepts negative numbers. Your code requires both the number of elements AND the zero sentinel, which does not meet the requirements of the task. If you insist on using an array rather than a collection class then the array should be sized to the maximum size, 50, and you need to keep track of the numbers accepted. https://code.sololearn.com/c61p47T8GR45 At no point does the problem state that the first entry should be the number of elements. EDIT: I think it may be the way the code playground is displaying the values... Inputs: 3 -12 -11 -10 3 2 1 0 Output: Enter array elements : -12, -11, -10 Please enter valid inputs Total array elements = 3 Inputs: 3 3 -3 2 -2 1 -11 -10 -12 0 Output: Enter array elements : 3, -3, 2 Please enter valid inputs Total array elements = 3
A.J. Your code still requires the number of elements to be entered as the first value. The purpose of the exercise is to accept an unknown number of positive values to a maximum of 50. At this time your code does the following... Inputs: 3 -12 -11 -10 3 2 1 0 Output: Enter array elements : Array Elements in Ascending Order : -12, -11, -10 Enter array elements : 2, 1 Array Elements in Ascending Order : 0, 1, 2 Exit value not found Total array elements = 6 Please study the two examples that I have provided. One uses a collection class while the other uses a fixed size array. If you want the number of entries accepted; for the array it is the value of count and for the ArrayList it is returned by the arr.size() method.
Eka, have you considered using the java.util.Arrays.sort() method? https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html Java has a huge API consisting of over 5,000 classes. Spending some time studying it is a good investment. Java provides classes and methods for many common situations leaving you to concentrate on the business logic of your application rather than the little things.
Eka, you do know that eclipse is not a compiler don't you? Eclipse is an Integrated Development Environment (IDE) implemented in Java. It will run with only the Java Runtime Edition (JRE) installed. However, to compile Java programs the Java Development Kit (JDK) is required.
Eka, since you require a variable size array the logical approach is to use a collection class. In the following example I have used an ArrayList... https://code.sololearn.com/c8rigK6sKcsl I chose to use the Collections.sort() method rather than the ArrayList.sort() method as it was easier to use in this situation.
Martin Taylor Now you can check and tell me if still there is some issues, I have resolved all problem. My input example is like this: 3 -12 11 10 4 1 3 2 7 5 1 -2 3 4 5 0 //exit value
You algorithm for ordering of the array items do not work. A easier algorithm is to set „tem“ with the first array item and thing it is bigest. And then check another etc. You can see a few sorting algorithms samples here: https://www.sololearn.com/learn/774/
Eka 2 mistakes: 1 - You are closing scanner object 'input' inside loop which will give exception 2 - there should be temp = num[k] instead of temp = num[i] So here is solution, I have created different methods for different operations. https://code.sololearn.com/cTa6XLUuvIRu/?ref=app
Martin Taylor Can you tell me when it is coming because I had entered input like this: 3 //no of elements 4 5 6 0 //to break do while loop And it worked fine without any exception And worked well on this inputs too: 5 4 -13 3 2 1 -12 0
Martin Taylor Yes I got that exception when we enter less number of elements. Now please can you check?
@A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟, your code throws an exception when I try it.