A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • one year ago

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.

  • This Question is Open
  1. alyssa_xo
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ``` //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 } ```

  2. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I wrote the functions that you will need. The rest you can easily do. https://gist.github.com/AlexeyNosych/cc23e780d58c5a67f279

  3. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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); } }

  4. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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); } }

  5. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.