Average an array) Write two overloaded methods that return the average of an array with the following headers: public static int average(int[] array) public static double average(double[] array) Write a test program that random generate ten integers between 0 – 100, and ten double values between 0 – 50. Invokes both methods, and displays all values and the average values. (All double numbers will be displayed with two-digit decimal places.) as i have done somewhat but, still there is error in it. please solve this problem according to the question.
``` //generation import java.util.Random; Random rng = new Random(); //random number generator (int) for(int i=0;i<10;i++) array[i] = rng.nextInt(101); //nextInt(n) generates an int from 0...n-1 ``` similarly use rng.nextDouble which generates a number from [0,1) ``` display(T arr) { //loop over arr size //print each element } avg(T arr) { sum=0 loop over array accumulate elements in arrayy and store in sum divide by size of collection } ```
I wrote the functions that you will need. The rest you can easily do. https://gist.github.com/AlexeyNosych/cc23e780d58c5a67f279
Thanx for your kind help but m still confused... i did something which is as follows ackage intdouble; /** * * @author JAY */ import static java.lang.Math.random; import java.util.Scanner; import java.util.Random; //import java.util.Random.nextint; public class IntDouble { public static int average(int[] array) { int sum = 0; int average = 0; for (int i = 0; i < array.length; i++) { random rn= new random(); sum += random.nextInt(100); } average = sum / array.length; return average; } public static double average(double[] array) { double sum = 0; double average = 0; for (int i = 0; i < 5; i++) { sum += random.nextInt(50); } average = sum / array.length; return average; } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter Integer values: "); int array1[] = new int[4]; for (int i = 0; i < array1.length; i++) { array1[i] = input.nextInt(); } int avg1 = average(array1); System.out.println("Average of First Array: " + avg1); System.out.println(); System.out.print("Enter Ten Double Values: "); double array2[] = new double[10]; for (int i = 0; i < array1.length; i++) { array2[i] = input.nextDouble(); } double avg2 = average(array2); System.out.println("Average of Second Array: " + avg2); } }
This is waht i did.. but it ask for number.. but i want to genrate the number randomly for both integer and double and and last it has to show those 10 random numbers.. Please complete this program... i am posting it again /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package intdouble; /** * * @author JAY */ import static java.lang.Math.random; import java.util.Scanner; import java.util.Random; //import java.util.Random.nextint; public class IntDouble { public static int IntDouble(int[] array) { int sum = 0; int average = 0; Random rn= new Random(); for (int i = 0; i < array.length; i++) array[i] = rn.nextInt(101); sum += Random.nextInt(100); average = sum / array.length; return average; } public static double IntDouble(double[] array) { double sum = 0; double average = 0; { Random rn= new Random(); for (int i = 0; i < 5; i++) array[i] = rn.nextInt(101); //sum += Random.nextInt(50); } average = sum / array.length; return average; } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter Integer values: "); int array1[] = new int[10]; for (int i = 0; i < array1.length; i++) { array1[i] = input.nextInt(); } int avg1 = IntDouble(array1); System.out.println("Average of First Array: " + avg1); System.out.println(); System.out.print("Enter Ten Double Values: "); double array2[] = new double[10]; for (int i = 0; i < array1.length; i++) { array2[i] = input.nextDouble(); } double avg2 = IntDouble(array2); System.out.println("Average of Second Array: " + avg2); } }
Join our real-time social learning platform and learn together with your friends!