Write a program that will do the following The main method s
Write a program that will do the following. The main method should ask the user how many numbers it will read in. It will then create an array of that size. Next, it will call a method that will read in the numbers and fill the array. Pass the array that was made in the main method as a parameter to this method. Use a loop that will read numbers from the keyboard and store them in the array. This will be a void method, since it changes the contents of the array but does not return a separate result. Next, the main method will call another method that will find the average of the array values. Pass the array as a parameter to this method. It will use a loop to add the numbers in the array to calculate the total. After the loop is finished divide the total by the number of array elements to get the average. Return this average to the main method. The main method will print the average. Include some identification to the number printed such as “The average is …”. The main method will call a method and pass the array as a parameter. The method will find the largest value of the array and return that value. Finally the main method will call another void method where entire array will be printed.
Solution
import java.util.*;
public class cheggex1 {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println(\"enter array limit:\");
int limit=sc.nextInt();
int arr[]=new int[limit];
int arr2[]=new int[arr.length];
arr2=fillarray(arr);
int avg=findavg(arr2);
System.out.println(avg);
int arr5[]=new int[arr.length];
int large=findlarge(arr2);
System.out.println(large);
for(int i=0;i<arr2.length;i++)
System.out.print(\"elements \"+arr2[i]);
}
private static int findlarge(int[] arr2) {
int arrla[]=new int[arr2.length];
arrla=arr2;
int large=0;
for(int i=0;i<arrla.length;i++){
if(arrla[i]>large)
large=arrla[i];
}
return large;
}
private static int findavg(int[] arr2) {
int arrvag[]=new int[arr2.length];
arrvag=arr2;
int sum=0;
int avg=0;
for(int i=0;i<arrvag.length;i++){
sum=sum+arrvag[i];
avg=sum/arrvag.length;
}
return avg;
}
private static int[] fillarray(int[] arr) {
int arr1[]=new int[arr.length];
System.out.println(arr1.length);
for(int i=0;i<arr1.length;i++){
System.out.println(\"enter elements:\");
arr1[i]=sc.nextInt();
}
return arr1;
}
}
