hello! i have a code that i am having trouble with. its for java selection sort, its about finding average, median and others. i have done most of the code but I know I am missing a few things since the outcome isn’t the same. the file has the info for the project and the code will be posted here.
package projectFour;
import java.util.Arrays;
import java.util.Random;
public class comp110ProjectFour {
public static void main(String[] args) {
Random rand = new Random();
int info[] = new int[20];
int number = rand.nextInt(5)+15;
for (int i = 0; i < number; i++)
{
info[i] = rand.nextInt(90) + 10;
}
System.out.println (“Student Name – Start Project 4”);
System.out.println (“n”);
System.out.println(“Array Size: ” + info.length);
System.out.println (“n”);
for (int i = 0; i < number; i++)
System.out.print(info[i] + ” “);
int aV = rand.nextInt(90) + 10;
compareNumbers(info, number, aV);
selectionSort(info, number);
System.out.println (“n”);
searchBinary(info, number, aV);
averageNumber(info, number);
medianNumber(info, number);
System.out.println (“n”);
System.out.println (“Student Name – End Project 4”);
}
public static void searchBinary(int[] info, int number, int aV)
// Search For Binary
{
System.out.print(“nBinary Search ” + aV + “: “);
int l = 0, r = number – 1;
while (l <= r) {
int mid = (l + r) / 2;
if (info[mid] == aV)
{
System.out.println(“Element found at ” + mid);
return;
}
if (info[mid] < aV) l = mid + 1;
else r = mid – 1;
}
System.out.println(“N/A”);
}
public static void selectionSort(int []info, int number) {
for (int i = 0; i < number – 1; i++) {
int k = i;
for (int j = i + 1; j < number; j++)
{
if (info[j] < info[k])
{
k = j;
}
}
int s = info[k];
info[k] = info[i];
info[i] = s;
}
System.out.println(“nArray after Selection Sort:-“);
for (int i = 0; i < number; i++) System.out.print(info[i] + “, “);
}
public static void compareNumbers(int []info, int number, int aV)
// Compare The Numbers
{
System.out.print(“nSearch Result for ” + aV + “: “);
for (int i = 0; i < number; i++) {
if (info[i] == aV)
{
System.out.println(“Found:” + i);
return;
}
}
System.out.println(“Not Found”);
}
public static void averageNumber(int []aV, int number)
// Searches For The Average
{
int sum = 0;
for (int i = 0; i < number; i++)
sum += aV [i];
int average = sum/number;
System.out.println(“Average:” + average);
}
public static void medianNumber(int []aV, int number)
// Searches For The Median
{
int med[] = new int[number];
System.arraycopy(aV, 0, med, 0, number);
Arrays.sort(med);
if (number % 2 == 0)
{
System.out.println(“Medians: ” + med[(number / 2)] + “, ” + med[(number / 2) – 1]);
}
else
{
System.out.println(“Median: ” + med[(number / 2)]);
}
}
}


0 comments