For assignment 6 you will be gaining more experience with loops while working wi
ID: 3676168 • Letter: F
Question
For assignment 6 you will be gaining more experience with loops while working with Arrays. The Java program you will write will create an Array with 10000 items. The type of the Array will be an Array of ints. After creating the array, your program will fill the array with random integers between 0 and 200. Then you will write a series of loops that will compute the sum, the average, the minimum value of the array and where it occurs, the maximum value of the array and where it occurs, and how many even and odd numbers occur in the array. After computing each of these values, print them to the console. PLEASE write a loop for each value to be computed or found. There should be 6 loops in total
. – (5 points) Create an array of integers with size 10000.
– (5 points) Write a loop to fill array with random numbers between 0 and 200. Use the same Random class you used in the Rock-Paper-Scissors assignment.
– (8 points) Write a loop to compute the sum of the array.
– (8 points) Write a loop to compute the average of the array.
– (8 points) Write a loop to find the minimum value of the array and where it occurs.
– (8 points) Write a loop to find the maximum value of the array and where it occurs.
– (8 points) Write a loop to count the number of even numbers and the number odd numbers in the array.
– (10 points) Please document/comment your code where necessary.
Sample Output:
The Sum is : 1007037
The average is : 100.7037
The minimum value is : 0 and its index is 556
The maximum value is : 200 and its index is 2569
The array contains 5051 even numbers.
The array contains 4949 odd numbers.
Explanation / Answer
ArrayOfIntegers.java
package org.students;
public class ArrayOfIntegers {
public static void main(String[] args) {
int sum = 0, sum1 = 0, num = 0, num1 = 0, num2 = 0, num3 = 0, even = 0, odd = 0, max = 0, min = 0, index = 0, index1 = 0;
double avg = 0;
// create an array of size 1000 which is of type integer.
int array[] = new int[10000];
// Fill the array with numbers between 0 and 200.
for (int i = 0; i < 10000; i++) {
array[i] = ((int) (Math.random() * 200));
}
// To find the sum of all the elements in the array.
for (int i = 0; i < 10000; i++) {
sum = sum + array[i];
}
System.out.println("The Sum all the elements in the array are :" + sum);
// To find the average of all the elements in the array.
for (int i = 0; i < 10000; i++) {
sum1 = sum1 + array[i];
avg = sum1 / (i + 1);
}
System.out.println("The average of all the numbers in the array is :"
+ avg);
// This will find out how many number of even numbers are there in this
// array
for (int i = 0; i < 10000; i++) {
num = array[i];
if (num % 2 == 0) {
even++;
}
}
System.out
.println("The total numbers of even numbers in the array are :"
+ even);
// This will find out how many number of odd numbers are there in this
// array
for (int i = 0; i < 10000; i++) {
num1 = array[i];
if (num1 % 2 != 0) {
odd++;
}
}
System.out.println("The total numbers of odd numbers in the array are :"
+ odd);
// This will find out the minimum number of all the elements in the
// array and its index.
min = array[0];
for (int i = 1; i < 10000; i++) {
num3 = array[i];
if (num3 < min) {
index1 = i;
min = num3;
}
}
System.out
.println("The minimum value of all the numbers in the array is :"
+ min + " and its index is :" + index1);
// This will find out the maximum number of all the elements in the
// array and its index.
for (int i = 0; i < 10000; i++) {
num2 = array[i];
if (num2 > max) {
index = i;
max = num2;
}
}
System.out
.println("The maximum value of all the numbers in the array is :"
+ max + " and its index is :" + index);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.