Question

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

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a. Write partial code to print all array elements of x in reverse order (one array element per line)
for( int j = MAX-1; j >=0; j-- )
{
System.out.println(x[j]);
}


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


double average = 0.0, sum = 0.0;
for( int j = 0; j < MAX; j++ )
{
sum = sum + x[j];
}

average = sum/MAX ;

Add a comment
Know the answer?
Add Answer to:
Use the following code, which initializes an array x with 1000 random integers, to answer the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT