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

Given an array of positive integers a, your task is to calculate the sum of every...
Given an array of positive integers a, your task is to calculate the sum of every possible a[i] ∘a[j], where a[i]∘a[j] is the concatenation of the string representations of a[i] and a[j] respectively. Example For a = [10, 2], the output should be concatenationsSum(a) = 1344. a[0] ∘a[0] = 10 ∘10 = 1010, a[0] ∘a[1] = 10 ∘2 = 102, a[1] ∘a[0] = 2 ∘10 = 210, a[1] ∘a[1] = 2 ∘2 = 22. So the sum is equal to...
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 simple MIPS Assembly program to average 3 integers. Make sure to read in three...
Write a simple MIPS Assembly program to average 3 integers. Make sure to read in three numbers from the user instead of hard coding them. To divide the sum by 3 you may use div $t0, $t0, 3 where register $t0 conatins the sum of the 3 integers. .data prompt1: .asciiz " Please enter an integer: " prompt2: .asciiz " Please enter an integer: " prompt3: .asciiz " Please enter an integer: " result: .asciiz "The average of three number...
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.)
Question: Not sure how to proceed with question below: In this task, task 2(a) will be...
Question: Not sure how to proceed with question below: In this task, task 2(a) will be extended so that an array of five Person objects are created. Write the comma separated values format of each object in separate lines in a file named persons.txt. Make use of the toString() method of Person to obtain comma separated values. Create a hyperlink pointing to the file persons.txt and include it on the task page. When the user clicks on the link, the...
*****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]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT