Proper outputs: Write a class called SortedArray.java. You can do all of this wo
ID: 3584345 • Letter: P
Question
Proper outputs:
Write a class called SortedArray.java. You can do all of this work in the main() method of your class but if there are places to use methods effectively, feel free to do so. Prompt the user to enter a number between 1 to 10 and create an array of integers that size. Re-prompt for out-out-bounds entries. Print the array of integers (should be all zeros) Prompt the user to enter numbers to be placed in the array until the array is "full". Here's the "tricky" part: Add the numbers to the array so that they remain sorted at all times. If the number is the biggest in the array it should go to the end. If the number is the smallest it should go to the beginning and all previously entered numbers should be moved to make room. If the number belongs in the middle somewhere put it where it belongs and move everything else down. It would be very helpful to keep a variable that tracks how many elements in the array are valid. The zeros at the beginning are just place holders in the array so your numElements variable should be 0 to start. After adding one number to the array it should go up to 1 and so on. This will help you to figure out where the new numbers should be placed and where the old numbers need to be shifted to and also allow you to ignore any zeros that are left over from creating the array (they are not valid data). After each user-entered number, display the updated array. The program ends when the array is full (numElements == array.length) and sorted. Enter number of values in the array (1 to 10):-1 Enter number of values in the array (1 to 10):20 Enter number of values in the array (1 to 10):5 Here's your starting array: 0 0 0 0 0 Enter an integer value: 5 Updated array: 5 0 0 0 0 Enter an integer value: 1 Updated array: 1 5 0 0 0 Enter an integer value: 4 Updated array: 1 4 5 0 0 Enter an integer value: 3 Updated array: 1 3 4 5 0 Enter an integer value: 2 Updated array: 1 2 3 4 5 Array full. Program complete.Explanation / Answer
import java.util.*;
public class array
{
public static void main(String[]args)
{
int num[]={5,4,3,2,1,0};
int l=num.length;
int i,j;
System.out.println("given number");
for(i=0;iSystem.out.println(""+num[i]);
}
System.out.println(" ");
System.out.println("ascending order");
Array.sort(num);
for(i=0;i{
System.out.println(" "+num[i]);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.