Question

In: Computer Science

1-Create a one-dimensional array of 99 double values. Then, use a for loop to add a...

1-Create a one-dimensional array of 99 double values. Then, use a for loop to add a random number from 0 to 100 to each element in the array. To do that, use the random method of the Math class to get a double value between 0.0 and 1.0 and multiply it by 100

like this

Math.random() * 100


2-Use an enhanced for loop to sum the values in the array. Then, calculate the average value and print that value on the console like this

Average: 50.9526671843517


3-Use the sort method of the Arrays class to sort the values in the array, and print the median value (the 50th value) on the console

like this

Median:    52.18369291650803
-Print the 9th value of the array on the console and every 9th value after that.

like this

position:    9         8.927702403161032

position:    18       14.0531287498060076

...

position:   99      22.471670293184822

Solutions

Expert Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

import java.util.Arrays;

public class DoubleRandom {

  

   public static void main(String[] args) {

      

       // creating array

       double[] arr = new double[99];

      

       for(int i=0; i<99; i++){

           arr[i] = Math.random() * 100;

       }

      

       double sum = 0;

       for(double d : arr){

           sum += d;

       }

      

       System.out.println("Average : "+sum/99.0);

      

       Arrays.sort(arr);

      

       System.out.println("Median: "+arr[49]); // 50th value

      

       int index = 8; // 9th value index

       while(index < 99){

           System.out.println("position: "+(index+ 1)+" "+arr[index]);

           index = index + 9;

       }

   }

}

/*

Sample run:

Average : 49.75761742464862

Median: 49.962842815728436

position: 9 8.766757546030624

position: 18 17.262110835701115

position: 27 20.865192789633745

position: 36 29.89303631952237

position: 45 43.084534673882814

position: 54 55.62839129243991

position: 63 66.13908523713565

position: 72 77.36692081739585

position: 81 83.77114421253107

position: 90 88.62289971975

position: 99 99.78514216085563

*/


Related Solutions

Create a function to output a one dimensional double array Mwith n elements where the...
Create a function to output a one dimensional double array M with n elements where the first three elements are 1 and each subsequent element is the sum of previous three elements before it. Name the function myArray. Write the function in the correct format to be used to create a Matlab function. Call the function in correct format to output the array with 7 elements.
In C++ using a single dimensional array Create a program that uses a for loop to...
In C++ using a single dimensional array Create a program that uses a for loop to input the day, the high temperature, and low temperature for each day of the week. The day, high, and low will be placed into three elements of the array. For each loop the day, high, and low will be placed into the next set of elements of the array. After the days and temps for all seven days have been entered into the array,...
In C++ for netbeans. Use a 1 dimensional array object to create a 2 dimensional table...
In C++ for netbeans. Use a 1 dimensional array object to create a 2 dimensional table object. Then modify to create a triangular table. Objective -> create an array of dynamic objects of RowAray inside Table i.e. an Aggregate. See Specs RowAray.h, Table.h Then create a triangular table, i.e. Triangle. Fill each cell with random 2 digit integers. The example Table has 8 columns of RowAray objects each filled with 6 rows of random 2 digit numbers. Then create a...
IN JAVA Create an array and add random values to it, and then find the sum...
IN JAVA Create an array and add random values to it, and then find the sum of the values using recursion.
Write a Java program to randomly create an array of 50 double values. Prompt the user...
Write a Java program to randomly create an array of 50 double values. Prompt the user to enter an index and prints the corresponding array value. Include exception handling that prevents the program from terminating if an out of range index is entered by the user. (HINT: The exception thrown will be ArrayIndexOutOfBounds) *Please do it in eclipse and this is java language*
In C++, Create an array of 10 characters. Use a loop to prompt for 10 letters...
In C++, Create an array of 10 characters. Use a loop to prompt for 10 letters (a-z|A-Z) and store each in the array. If an invalid character is entered, re-prompt until a valid char is entered. After 10 characters are stored in the array, use a loop to print out all characters in the array from the first to the last element. Then, use another loop starting at the last element to print all characters in the array in reverse...
Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0.
This program is for C.Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0. Combine the elements of A + B to create two- dimensional array C = A + B. Display array A, B and C to the screen for comparison. (Note a[0] + b[0] = c[0], a[1] + b[1] = c[1], etc.)
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers as follows: Assign {35,20,-43,-10,6,7,13} -Create a one dimensional array of 7 Boolean values as follows: Assign {true,false,false,true,false,true,false} -Create a one dimensional array of 7 floating-point values as follows: Assign {12.0f,1.5f,-3.5f,-2.54f,3.4f,45.34f,22.13f} -Declare sum as integer and set it to 0. -Declare sumf as float and set it to 0.0f. -Use a for loop to go through each element of the Boolean array, and if an...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
c++ (1) Create a class, named Board, containing at least one data member (two-dimensional array) to...
c++ (1) Create a class, named Board, containing at least one data member (two-dimensional array) to store game states and at least two member functions for adding players’ moves and printing the game board. Write a driver to test your class. (2). The data type of the two-dimensional array can be either int or char. As a passlevel program, the size of the board can be hardcoded to 10 or a constant with a pre-set value, anything between 4 and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT