Question
Define an array, Read values into array, Compute average of all elements of array, Computer Max and Min of elements of array, Print an array,
Define an array, Read values into array, Compute average of all elements of array, Computer Max and Min of elements of array, Print an array, etc.
Consider the following example --
import java.util.Scanner;
public class TestArray {
/** Main method */
public static void main(String[] args) {
final int SIZE = 6;
int[] numbers = new int[SIZE];
Scanner kb = new Scanner(System.in); // kb is the Scanner object name J
// Read all numbers
for (int i = 0; i < numbers.length; i++) {
System.out.print("Enter a number:");
numbers[i] = kb.nextInt(); // we used kb as defined in Scanner object }
//find the average of all numbers
double average = 0.0;
int sum = 0;
for (int I = 0; I < numbers.length; i++) {
sum = sum + numbers[i];
}
if (numbers.length != 0) average = (double) sum / numbers.length;
//write code here to print average value here
// Find the largest
int max = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i]> max)
max = numbers[i];
}
//write code here to print max value here similarly compute min and print
// Find the number of occurrences of the largest number
int count = 0;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == max) count++;
}
// Print the result
System.out.println("The array values are shown below ";
for (int i = 0; i < numbers.length; i++) {
System.out.println( numbers[i]);
}
System.out.println( " The largest number is " + max) ;
System.out.println(" The occurrence count of the largest number "
+ "is " + count) ;
System.out.println( " That is all Folks!! J ");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started