Read several (at least one) positive integers and display all of the integers which are greater than their average value.
For example, the input:
10
4
15
7
- 1
Yields the output:
10
15
Our solution will feature the global procedural architecture.
/ /
The main method.
static public void main (~ ~ ~ ~)
{
readAndStoreTheNumbers ();
computeTheAverage ();
displayTheNumbersAboveAverage ();
/ /
Data Dictionary
static private int a [];
static private final int LIMIT = 1000;
static private double average;
static private int nrElements;
static private int number;
/ /
Refine the methods.
static private void readAndStoreTheNumbers ()
{
a
=
new int [LIMIT];
nrElements
=
0;
number
=
IO.read_int ();
while (number
>
0)
{
a [nrElements]
=
number;
nrElements
=
nrElements
+
1;
number
=
IO.read_int ();
}
}
static private void computeTheAverage ()
{
int sum
=
0;
for (int
x
=
0;
x
<
nrElements;
x + +)
{
sum
=
sum
+
a [x];
}
average
=
(double) sum
/
(double) nrElements;
}
static private void displayTheNumbersAboveAverage ()
{
for (int
x
=
0;
x
<
nrElements;
x
=
x
+
1)
{
if (a [x]
>
average)
{
IO.println (a [x]);
}
}
|