Main topics: Arrays Effective size Searching arrays Sorting arrays Exercise This lab is designed to give you practice wo
Posted: Thu May 05, 2022 1:14 pm
Main topics: Arrays Effective size Searching arrays Sorting arrays Exercise This lab is designed to give you practice working with arrays. Specifically you will be searching and sorting an existing array of values. Getting Started To start this exercise, you should 1. Open eclipse and start a new Java project named Lab11 2. Add a Class (named Lab11) to this project. 3. Cut the program from this document and paste (replacing everything) in the Eclipse editor window. Lab11.Java import java.util.Scanner; public class Lab11 { public static void main(String[] args) { Scanner stdIn new Scanner (System.in); final int Size = 10; double scores new double [Size]; // set all the scores to a random number [0-99] for (int = 0; s < Size; ++s) scores [s] = (int) (Math.random() 100); // print the scores - original order System.out.println("Scores Original"); System.out.println("-- print Array (scores, Size); System.out.println("\n"); // sort the scores System.out.println("Sorting.\n"); // print the scores sorted order System.out.println("Scores - Sorted"); System.out.println("------ ---------); print Array (scores, Size);
System.out.println("\n"); atdIn.close(); } public static void printArray (double [] arr, int eSize) ( for (int i = 0; i < eSize; ++i) System.out.println(arr); } public static int findMinLoc (double arr, int startInd, int endInd) { int minLoc startInd; for (int i startInd+ 1; i < endInd; ++i) { if (arr<arr [minLoc]) minLoc i; } return minloc; } public static void swapInArray (double [] arr, int indi, int ind2) ( double tmp arr [indi]; arr [indi] arr[ind2); arr [ind2] = tmp; } public static void sortArray (double [] arr, int eSize) ( for (int i = 0; i < eSize; ++i) { int ind findMinLoc (arr, i, eSize-1); swapInArray (arr, i, ind); } Problem Description . For this exercise you will be using the Lab11.java file that you copied using the command above. • In this file, we have provided you with a fully functional program that: - Declares an array of doubles. -Sets each element in the array to a random number. - Prints the array elements to the screen.
System.out.println("\n"); atdIn.close(); } public static void printArray (double [] arr, int eSize) ( for (int i = 0; i < eSize; ++i) System.out.println(arr); } public static int findMinLoc (double arr, int startInd, int endInd) { int minLoc startInd; for (int i startInd+ 1; i < endInd; ++i) { if (arr<arr [minLoc]) minLoc i; } return minloc; } public static void swapInArray (double [] arr, int indi, int ind2) ( double tmp arr [indi]; arr [indi] arr[ind2); arr [ind2] = tmp; } public static void sortArray (double [] arr, int eSize) ( for (int i = 0; i < eSize; ++i) { int ind findMinLoc (arr, i, eSize-1); swapInArray (arr, i, ind); } Problem Description . For this exercise you will be using the Lab11.java file that you copied using the command above. • In this file, we have provided you with a fully functional program that: - Declares an array of doubles. -Sets each element in the array to a random number. - Prints the array elements to the screen.