• 03-29-2010, 02:22 PM
light-flyer
Hey guys, im new here and need some help as i see that many of you here are good at java.

Heres the prompt:

Write a program that takes 10 numbers as inputs. The program then displays the average of the numbers followed by all of the numbers that are greater than the average. As part of your design, write a method that takes an array of doubles as a parameter and returns the average of the data in the array

I have tried to do as much as i can and ill link it below but i am no good at using methods, let alone passing values to and from it. So all they help is really appreciated. Could you also let me know if im not following the prompt correctly or mis-read it.

Thanks

Heres what i have so far:

import java.util.Arrays;
import java.util.Scanner;

public class AverageNumbers {

public static void main (String[]args){

Scanner reader = new Scanner (System.in);

int size = 5;
double sum = 0, average;
double [] numArray = new double [size];

for(int i = 0; i < numArray.length; i++){
}

for(int i = 0; i < numArray.length; i++){
sum += numArray[i];
}

average = sum / size;

System.out .println("Average: " + average);

for(int i = 0; i < numArray.length; i++){
if(numArray[i]>average){
System.out.println("These numbers are greater than the average: " + numArray[i]);
}

}

}
}
• 03-29-2010, 02:32 PM
PhHein
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

Use code tags when posting code, that is &#91;CODE]your code goes here&#91;/CODE]
• 03-29-2010, 02:34 PM
JosAH
Quote:

Originally Posted by light-flyer
Write a program that takes 10 numbers as inputs.
Code:

```                int size = 5;                 double [] numArray = new double [size];```

I guess your assignment meant 10 as in 10 decimal, not as in 10 base 5. ;-)

kind regards,

Jos
• 03-29-2010, 02:43 PM
light-flyer
oh sorry about that PhHein, will do thanks.

JosAh , i did mean 10 but as im testing it im saving the trouble and time by just entering 5 integers at first.
• 03-29-2010, 03:34 PM
JosAH
Quote:

Originally Posted by light-flyer
JosAh , i did mean 10 but as im testing it im saving the trouble and time by just entering 5 integers at first.

I was only joking, but seriously now: what actually are your problems? Creating methods? Here is one method skeleton: it is supposed to read the numbers for your array; you already have that code, you only should move it into the following method:

Code:

```public static void readNumbers(double[] array) {   // read numbers into the array }```

In your main( ... ) method you should call this method if you want numbers read into your array:

Code:

```// somewhere in the body of your main( ... ) method readNumbers(numArray);```
kind regards,

Jos