Question

In: Computer Science

Task #3 Experimenting with an array of random integers (Week 2) Make sure that the following...

Task #3 Experimenting with an array of random integers (Week 2)
Make sure that the following 3 experiments are done repeatedly by generating a new set of integers to fill the array. For this iterate 10 times and observe results for consistency.
• Make the array size reasonably large (say a thousand) and an even number.
• Choose the version of nextInt() that allows you to set a cap on the random integers generated (set it to a few thousands, but way less than the maximum integer).
• Do the following experiments:
o Count how many integers in the array are odd and even. Are the odd and even counts almost equal?
o Find the minimum, maximum, and average of the integers in the array. Keep track of the minimum and maximum of the three quantities across runs.
o Sort the array by writing an inner and an outer loop. Count the number of times you needed to swap integers.

Solutions

Expert Solution

Code - Main.java

import java.util.*;
public class Main
{
   public static void main(String[] args) {
   //initialize array of 1000 number
   int a[] = new int[1000];
   //variable declare to be used
   int countEven=0,countOdd=0,max,min=99999,average,sum=0,i,j,swapCount=0;
   //creat Random object to generate random number
   Random rand = new Random();
for( i = 0 ; i <a.length;i++){
//random number will be generated from 0 to 999 and will store in array a[]
a[i] = rand.nextInt(1000);
}
//initialize max to a[0]
max = a[0];
//for loop to get the max , min, sum , even count , odd count
for(i = 0 ; i <a.length;i++){
//count even
if(a[i]%2==0)
countEven++;
//count odd
else
countOdd++;
//get the max
if(max<a[i])
max = a[i];
//get the min
if(min>a[i])
min = a[i];
//calculate the sum
sum+=a[i];
}
//sorting the array using bubble sort alorithm
for ( i = 0; i < a.length-1; i++){
for ( j = 0; j < a.length-1-i; j++){
if (a[j] > a[j+1])
{
//count the number of swaps
swapCount++;
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
//print the result
System.out.println("Even number count is "+countEven);
System.out.println("Odd number count is "+countOdd);
System.out.println("Max number is "+max);
System.out.println("Min number is "+min);
System.out.println("Average is "+(sum/a.length));
System.out.println("Swap count for sorting "+swapCount);
  
   }
}

Screenshots -


Related Solutions

write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
Write a program that does the following: Generate an array of 20 random integers between -100...
Write a program that does the following: Generate an array of 20 random integers between -100 and 100. Compute the average of the elements of the array and find the number of elements which are above the average. For example, if the elements of the array were 5 2 4 1 3 then your program should output The average is 3.0 There are two elements above the average Find the smallest element of the array as well as its index...
Using Java please You are given an array of integers arr. Your task is to count...
Using Java please You are given an array of integers arr. Your task is to count the number of contiguous subarrays, such that each element of the subarray appears at least twice. E.g For arr = [0, 0, 0], the output should be duplicatesOnSegment(arr) = 3.
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.)
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
Write a function called randFill that fills the entries of an array with random integers in...
Write a function called randFill that fills the entries of an array with random integers in the range from 10 to 99 (inclusive). (You should use a standard Java method to generate the values. Your solution should use no more than 6 lines of code.) For example, a program that uses the function randFill follows. public class P4 { public static void main(String args[]) { int x[]; x = randFill(5); for (int i = 0; i < 5; i++) System.out.print(x[i]...
Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1...
Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1 and 100 inclusive Then, creates two more 100-element arrays, one holding odd values and the other holding even values. Prints both of the new arrays to the console. In C++. Thank you!
Q.1: Use the NumPy’s random number generation to create an array of five random integers that...
Q.1: Use the NumPy’s random number generation to create an array of five random integers that represent summertime temperatures in the range 60–100, then perform the following tasks: a. Convert the array into the Series named temperatures and display it. b. Determine the lowest, highest and average temperatures. c. Produce descriptive statistics for the Series. Q.2: Given the following dictionary; temps = {'Mon': [68, 89], 'Tue': [71, 93], 'Wed': [66, 82], 'Thu': [75, 97], 'Fri': [62, 79]} perform the following...
Write a program that initializes an array of 6 random integers and then prints 4 lines...
Write a program that initializes an array of 6 random integers and then prints 4 lines of output, containing the following: 1. Only the first and last element 2. Every element at an odd index 3. Every odd element 4. All elements in reverse order
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT