In class Assignment 16 Thursday Exercise A Total 15 Points W
In class Assignment #16 (Thursday)
Exercise A: (Total 15 Points)
Write one Java console program and printout the following exercises with the expected output.
Use this array for all the following exercises:
int[] myArray = { 45, 38, 27, 46, 81, 72, 56, 61, 20, 48, 76, 91, 57, 35, 78};
find the total number of elements in myArray
find the sum of all the elements of the above array
find the largest value of the array
find the index of the largest value of the array
find the smallest value
find the index of the smallest value
find the average of all number
count and display, how many numbers are above average
count and display how many numbers are below average
find the total even numbers in the the above array
find the sum of all the even numbers
find the total odd numbers in the above array
find the sum of all odd numbers
Sort the array in ascending order (2 points)
Expected output: (It can be in any order)
NOTE: you must submit the screen shot of the output
Expected Output:
Solution
import java.util.ArrayList;
public class MyArray{
static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String[] args) {
int[] myArray = {45, 38, 27, 46, 81, 72, 56, 61, 20, 48, 76, 91, 57, 35, 78};
//total number of elements
System.out.println(\"Total number of elements: \"+myArray.length);
//finding sum of elements
int sum = 0;
for (int i=0; i<myArray.length; i++){
sum += myArray[i];
}
System.out.println(\"Sum of elements: \"+sum);
//finding largest value
int largest = -1000;
int j = -1;
for(int i=0; i<myArray.length; i++){
if (myArray[i]>largest){
largest = myArray[i];
j = i;
}
}
System.out.println(\"Largest element: \"+largest);
System.out.println(\"index of the largest value of the array: \"+j);
//finding smallest value
int smallest = 1000;
j = -1;
for(int i=0; i<myArray.length; i++){
if (myArray[i]<smallest){
smallest = myArray[i];
j = i;
}
}
System.out.println(\"smallest element: \"+smallest);
System.out.println(\"index of the smallest value of the array: \"+j);
//Average
float average = sum/myArray.length;
System.out.println(\"Average: \"+average);
int aboveAverage = 0;
int belowAverage = 0;
for(int i=0; i<myArray.length; i++){
if (myArray[i]<average){
belowAverage++;
}
else if(myArray[i]>average){
aboveAverage++;
}
}
System.out.println(\"Above Average: \"+aboveAverage);
System.out.println(\"Below Average: \"+belowAverage);
//Finding even and odd numbers
int sumEven = 0;
int numEven = 0;
int sumOdd = 0;
int numOdd = 0;
for(int i=0; i<myArray.length; i++){
if (myArray[i]%2==0){
numEven++;
sumEven += myArray[i];
}
else{
numOdd++;
sumOdd += myArray[i];
}
}
System.out.println(\"Total number of even numbers: \"+numEven);
System.out.println(\"Sum of even numbers: \"+sumEven);
System.out.println(\"Total number of odd numbers: \"+numOdd);
System.out.println(\"Sum of odd numbers: \"+sumOdd);
bubbleSort(myArray);
System.out.print(\"Sorted Array: \");
for (int elem: myArray){
System.out.print(elem+\", \");
}
System.out.println();
}
}


