Question

Write a Java program that: 1. Creates an array of 500 integers. Initialize the array to...

Write a Java program that:

1. Creates an array of 500 integers. Initialize the array to have all zeros.

2. Puts odd numbers starting from 19 (excluding multiples of 7) into the first 250 slots of the array above. So numbers 19,23,25,27,29,31,33,37, etc. go into the first 250 positions in the array. Print the array.

3. Puts random numbers between 5 and 35 into the second 250 slots of the array above (second 250 positions). Print the array.

4. Shuffle the contents of the array. Print the array.

5. Find the average of all the numbers from the 50th index to the 250th index in the array.

6. Print the average to two decimal places of accuracy to the right of the decimal. Meaning something like this 12.34

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

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

import java.util.Random;
public class Main
{
// Print a bottom-right right-angled triangle of stars, of size SIZE
public static int[] RandomizeArray(int[] array,Random rgen){

       for (int i=0; i<array.length; i++) {
       int randomPosition = rgen.nextInt(array.length);
       int temp = array[i];
       array[i] = array[randomPosition];
       array[randomPosition] = temp;
       }

       return array;
   }

public static void main(String[] args)
{
int[] arr=new int[500];
for(int i=0;i<500;i++)
{
arr[i]=0;
}
int num=19;
for(int i=0;i<250;i++)
{
if(num%7==0)
num=num+2;
arr[i]=num;
num=num+2;
}
Random rand = new Random();

for(int i=250;i<500;i++)
{
arr[i]=rand.nextInt(31)+5;
}
  
arr=RandomizeArray(arr,rand);
System.out.println("The array is ");
for(int i=0;i<500;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println("");
double sum=0;
for(int i=50;i<=250;i++)
{
sum=sum+arr[i];
}
sum=sum/(double)(250-49);
System.out.printf("The average is %.2f\n",sum);
}
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Write a Java program that: 1. Creates an array of 500 integers. Initialize the array to...
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