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.
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 ;
Use the following code, which initializes an array x with 1000 random integers, to answer the...
Write a program that initializes an array with ten random integers and then prints out the following: Every element at an even index; Every even element All elements in reverse order; Only the first and last elements; The minimum and maximum element The sum of all elements The alternating sum of all elements, where the alternating sum contains all elements at even index added, and the elements at odd index subtracted. Please write comments above the piece of code that...
Suppose v is an array with 100 int elements. If 100 is assigned to v[100], what happens? Show the code. An array of 1000 integers is declared. What is the largest integer that can be used as an index to the array? Shows the code. Consider the declaration: int v[1]; What is the index of the last element of this array? Declare an array named a of 10 int elements and initialize the elements (starting with the first) to the...
Java question Q1) Use the following code snippet which generates a random sized array with random contents to complete the following problems: public int [] createRandomArray() { int size = (int) (Math.random() * 10) + 1; int[] array = new int [size]; for (int i = 0; i < array.length; i++) { array[i] = (int) (Math.random() * 10 ) + 1; } return array; } Assignment...
My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...
Write a JAVA program with methods that initializes an array with 20 random integers between 1 and 50 and then prints four lines of output, containing 1)The initialized array. 2)Every element at an even index. 3)Every even element. 4)All elements in reverse order. Requirements (and hints): 1. Build a method that takes any integer array as input and prints the elements of the array. ALL printing in this lab must be done using a call to the printArray method and...
Rules to follow are:Declare an array with 1000 elements of type intThen in a loop generate 1000 random numbers and assign one to each element in the arrayThe random numbers should be between 1 and 50do not use rand or srand, use code is providedThen, prompt the user to enter a number, store this number in a local variable, the number should be safety checked using the GetInteger() functionThen, iterate through the array and determine how many times the user's...
need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...
Code in C - CountingSort exact as the algorithm which takes in an array of 1000 random integers. Provide comments in code for understanding purpose COUNTING-SORT(A, B, k) 1 let C[0..k] be a new array 2 for i = 0 to k C[i] = 0 4 for j = 1 to A.length 5 C[Ajl] = C[A;l] + 1 6 / C[i] now contains the number of elements equal to i. 7 for i = 1 tok C[i] = C[i] +...
Write array methods that carry out the following tasks for an array of integers by completing the ArrayMethods class below. For each method, provide a test program. public class ArrayMethods { private int[] values; public ArrayMethods(int[] initialValues) { values = initialValues; } public void swapFirstAndLast() { . . . } public void shiftRight() { . . . } .. . } a. Swap the first and last elements in the array. b. Shift all elements to the right by one...
(+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100) to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0 (how many) Display the number of integers < 0 (how many) Display the...