use methods NAME Problem: Develop a program which allows the user to enter numbe
ID: 3687886 • Letter: U
Question
use methods
NAME Problem: Develop a program which allows the user to enter numbers into an array Input will be as follows: The user will enter the total number of integers to be entered into the array The user will then enter that number of unique integers (negative or positive) entered to exceed the array size. tive or positive). Do not allow the number of values Do not allow the number of values Develop methods to: main method Print the array Sort the array ( YOU MUST DEVELOP YOUR OWN SORT METHOD -don't use someone else's) Determine the highest valuc Determine the lowest value Calculate the average value (double)Explanation / Answer
import java.util.Scanner;
class test
{
public static void main(String args[])
{
int n;
test t = new test();
Scanner s = new Scanner(System.in);
//get size of array
System.out.println("enter size");
n=s.nextInt();
//declare array with given size
int array[] = new int[n];
//input values into array
System.out.println("enter unique values into array");
for(int i=0;i<n;i++)
array[i] = s.nextInt();
//print array values
System.out.println("array before sort: ");
t.printArray(n,array);
//call function to sort values in array
t.sortArray(n,array);
//print array values
System.out.println("array after sort: ");
t.printArray(n,array);
//print highest value in array
System.out.println("highest value: "+array[n-1]);
//print lowest value in array
System.out.println("lowest value: "+array[0]);
//print average of values in array
System.out.println("average: "+avg(n,array));
}
private static double avg(int n, int[] array) {
double a=0,tot=0;
for(int i=0;i<n;i++)
{
tot = tot + array[i];
}
a = tot/n;
return a;
}
private void printArray(int n,int[] array) {
for(int i=0;i<n;i++)
System.out.print(array[i]+" ");
System.out.println("");
}
private void sortArray(int n, int[] array) {
int k,temp;
for (int m = n; m >= 0; m--) {
for (int i = 0; i < n - 1; i++) {
k = i + 1;
if (array[i] > array[k]) {
temp = array[i];
array[i] = array[k];
array[k] = temp;
}
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.