Use the following code which initializes an array x with 100

Use the following code, which initializes an array x with 1000 random integers, to answer the questions a and b.

java.util.Random generator = new java.util.Random( );//accessing Random class
final int MAX = 1000;
int[] x = new int[MAX];
for( int j = 0; j < MAX; j++ )
{
x[j] = generator.nextInt( MAX ) + 1;
}

a. Write partial code to print all array elements of x in reverse order (one array element per line)

b. Write partial code to compute the average of all elements and store it into a double variable named average.

Solution

Partial code for bit-a:

for( int j = MAX-1; j >=0; j-- )
{
System.out.println(x[j]);
}

Partial code for bit-b:

double sum=0.00,average;
for( int j = 0; j < MAX; j++ )
{
sum=sum+x[j];
}
average=sum/MAX;
System.out.println(\" Average of all numbers : \"+average);

///______________________________

Here is full code

class RandomGen
   {
       public static void main(String arg[])
           {
           java.util.Random generator = new java.util.Random( );//accessing Random class
           final int MAX = 1000;
           int[] x = new int[MAX];
           for( int j = 0; j < MAX; j++ )
               {
               x[j] = generator.nextInt( MAX ) + 1;
               }
       for( int j = MAX-1; j >=0; j-- )
{
System.out.println(x[j]);
               }
double sum=0.00,average;
for( int j = 0; j < MAX; j++ )
{
sum=sum+x[j];
}
average=sum/MAX;
System.out.println(\" Average of all numbers : \"+average);

       }
}

Use the following code, which initializes an array x with 1000 random integers, to answer the questions a and b. java.util.Random generator = new java.util.Rand
Use the following code, which initializes an array x with 1000 random integers, to answer the questions a and b. java.util.Random generator = new java.util.Rand

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site