package hw3;
public class Main {
public static void main(String[] args) {
final int NumberOfElements = 1000;
int[] num = new int[NumberOfElements];
int var = 0;
//create input
java.util.Scanner input = new java.util.Scanner(System.in);
for (int i = 0; i < NumberOfElements; i++) {
System.out.print("Enter any positive number or enter 0 to stop: ");
num[i] = input.nextInt();
var++;
if (num[i] == 0)
break;
}
Arrays.sort( num, 0, var);
int i;
for (i = 0; i < var; i++) {
System.out.print(" " + num[i]);
}
}
}
Write a Java program reading a sequence of positive integers entered one per line. The program stops reading when integer ‘0’ is entered. The program will sort and output them into an ascending numerical order.For example: 5
1
7
12
36
8
0
Output: 1 5 7 8 12 36
Try to understand the problem: there are three steps.
Step 1: Get the input from the user. Continue getting input till the user enters 0
Step 2: Sort
Step 3: Print out the results
Step 1: Your
forloop is almost correct.But the for loop should end immediately after you have got the input.
In the following code,
you are adding the user input to the array and then checking whether it is
0. This means that the0will also be part of your array. If you don’t want this, you should check the input and add it to the array only if it is not0.Also note the declaration of
ibefore theforloop. Because we need it after the for loop. Why? see below.Step 2: Sort it
Arrays.sort(num);Step 3: Print the output:
The problem is, in step 2, an array of 1000 elements is sorted, while you actually need to consider only the number of elements the user has entered. You don’t know that initially thats why you created an array of 1000 elements.But, at this point (after step 2) you do know how many number of elements the user has entered. This is present in
iSo new step between 1 and 2: Create a new arrays that contains only those elements that the user has entered.
Step 1.5:
Now sort this new array, and print it (same as your code, but uses the new array we just created)
Arrays.sort(newArray);Notes:
1. The ideal way to do this is to use Lists and not arrays, but probably since this is homework, you might have to use arrays.
Arrays.sortorArrays.copy. Your professor might frown at this because perhaps his intention was that you learn the constructs of the language viafor,ifandwhile. In that case you have to do step 1.5 (make array the right size) and sort by yourself.This is not difficult (but just remember this is not the best way to do it, except for in a homework)
Copy array (homemade) (in place of step 1.4 above)
Sort (homemade) (in place of step 2 above):
There are two loops because you have to do the comparison on a continuous basis: the the first element, compare with the entire array, place it in the right position, take the second compare with the entire array etc…)
Try to understand what really is happening, instead of just copy pasting.
Once you understand the homemade sort logic, think about this:
The second for loop in the sort
or(int k=0; k<newArray.length; k++) {can actually just befor(int k=0; k<newArray.length; k++) {. Why?Your print loop will remain the way you wrote it, but you will print
newArrayrather thannum. You might want to change the loop variable toint jor something, butiwill also work. (iholds the no of inputs now, so I would not use it for any other purpose. But it is just a manner of coding. Technically no difference – the code will work the same way)I am not combining the parts. I leave that o you, otherwise it will look like I did your homework 🙂