|
Assignment 14
Task
Store and sort a list of user-entered integers.
Concepts: Review of arrays and loops.
Steps
Ask the user to enter a series of integers. Store these integers in an int[] . When you declare this array, give it an initial size of 5 elements. Each time you would exceed the current capacity of the array, instead replace the array with a new array of double the size.
When the user is done (indicated by entering nothing--as with A11), print on one line all the numbers they entered in sorted order: highest to lowest value.
Sample Output
This program will sort the numbers you enter.
Enter an integer (or nothing to stop): 0
Enter an integer (or nothing to stop): 12
Enter an integer (or nothing to stop): 5
Enter an integer (or nothing to stop): -2
Enter an integer (or nothing to stop): 12.5
Bad input: please enter only whole numbers (or, to quit, just hit enter).
Enter an integer (or nothing to stop): nothing
Bad input: please enter only whole numbers (or, to quit, just hit enter).
Enter an integer (or nothing to stop): 5
Enter an integer (or nothing to stop):
You entered: 12 5 5 0 -2
Sorting Hints
Note that sorting is worth only 0.5 points (which is less than the late penalty), so worry about this last. Remember that you only need to print the numbers in sorted order, not necessarily get the array itself into that sorted order. Also, if you haven't filled your array to capacity, it will still contain 0s in the unused elements; be careful not to sort these in among your real data.
Here are some different ideas on how you might achieve sorted output:
What to Submit
Upload your UsernameA14.java file to Tamarin.
Grading [5 points]
- 1 - Compiles + Coding Standards
- Your program compiles successfully (no errors). Your code follows Java coding standards.
- 1.5 - Array
- Your program uses an
int[] to store the numbers read in from the user (0.5). You initially declare this array to be of size 5 (0.5). You double its size (and copy over all elements) each time the array capacity is exceeded (0.5).
- 2.0 - Input/Output
- Your program prompts the user to enter integers, stopping if the user enters nothing (empty string) (0.5). Your program ignores bad input and prompts the user again (0.5). Your program then prints out a list on one line of all the numbers the user entered (1.0).
- 0.5 - Correctly sorted.
- When the program prints out the entered numbers, they are in sorted order, from highest to lowest value.
FAQs
- Something to consider
- Image trying to do this assignment without arrays, but only with a number of int variables. First, sorting would be a nightmare (remember dealing with only 3 numbers in A06?), especially since you wouldn't be able to use a loop to traverse a series of separate variables. Secondly, you would be limited by the numbers of variables you defined. To handle a list of 10 numbers, you'd have to declare 10 variables, and (unlike arrays) there'd be no way to change this limit while the program is running.
- Argh, I'm stuck! How do I add the numbers to the array? How do I "double the size of the array"?
- For those in sections 001 and 002, I went over most of this in lab. Here's a summary. (The grey //comments refer to code you'll have to figure out on your own, which may take multiple lines.)
int nums = new int[5];
int count = 0; //tracks how many numbers have been added to nums
//while user keeps entering numbers
int number = //user's number
if (count == nums.length) {
//nums array is full, so we need to double it's size
int temp[] = new int[nums.length * 2]; //larger temporary array
//copy each value from nums into temp
nums = temp; //replace nums with larger temp array
}
//add number to the end of nums
nums[count] = number;
count++;
//end while
//print entered numbers
Note that this code does not handle the sorting yet.
- My numbers are coming out as all 0s.
- Be sure you declare your
nums array before the input loop, or else it'll get recreated each time through the loop.
- If I enter a negative number, my sorted output includes a bunch of extra 0s.
- Sounds like you confused
nums.length and count while sorting. You want to sort only those numbers entered by the user, not all the numbers in your array.
|