Exercise This lab is designed to give you practice working with arrays. Specific
ID: 656380 • Letter: E
Question
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.
Lab12.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 s = 0; s < Size; ++s)
scores[s] = (int)(Math.random() * 100);
// print the scores - original order System.out.println("Scores - Original"); System.out.println("------------------"); printArray(scores, Size); System.out.println(" ");
// sort the scores
System.out.println("Sorting ... ");
// print the scores - sorted order
System.out.println("Scores - Sorted");
System.out.println("----------------"); printArray(scores, Size); System.out.println(" ");
stdIn.close();
}
public static void printArray(double [] arr, int eSize)
{
for (int i = 0; i < eSize; ++i) System.out.println(arr[i]);
}
public static int findMinLoc(double [] arr, int startInd, int endInd)
{
int minLoc = startInd;
for (int i = startInd; i <= endInd; ++i)
{
if (arr[i] < arr[minLoc])
minLoc = i;
}
return minLoc;
}
public static void swapInArray(double [] arr, int ind1, int ind2)
{
double tmp = arr[ind1]; arr[ind1] = 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
Explanation / Answer
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 s = 0; s < Size; ++s)
scores[s] = (int)(Math.random() * 100);
System.out.println("Sorting ... ");
// print the scores - sorted order
System.out.println("Scores - Sorted");
System.out.println("----------------");
printArray(scores, Size);
System.out.println(" ");
stdIn.close();
}
public static void printArray(double [] arr, int eSize)
{
for (int i = 0; i < eSize; ++i)
System.out.println(arr[i]);
}
public static int findMinLoc(double [] arr, int startInd, int endInd)
{
int minLoc = startInd;
for (int i = startInd; i <= endInd; ++i)
{
if (arr[i] <= arr[minLoc])
minLoc = i;
}
return minLoc;
}
public static void swapInArray(double [] arr, int ind1, int ind2)
{
arr[ind1] = arr[ind1] + arr[ind2];
arr[ind2] = arr[ind1] - arr[ind2];
arr[ind1] = arr[ind1] - arr[ind2];
}
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);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.