Question

In: Computer Science

// JavaLanguage . You need to write a program that asks the user for an array...

// JavaLanguage

. You need to write a program that asks the user for an array size, and then asks the user to enter that many integers. Your program will then print out the sum , average, largest and smallest of the values in the array.

Solutions

Expert Solution

//SumAverageMinMaxArray.java
import java.util.Random;
import java.util.Scanner;

public class SumAverageMinMaxArray {
    public static int getLowest(int[] numbers){
        int min = Integer.MAX_VALUE;
        for(int i = 0;i<numbers.length;i++){
            if(min > numbers[i]){
                min = numbers[i];
            }
        }
        return min;
    }

    public static int getHighest(int[] numbers){
        int max = Integer.MIN_VALUE;
        for(int i = 0;i<numbers.length;i++){
            if(max < numbers[i]){
                max = numbers[i];
            }
        }
        return max;
    }

    public static int getTotal(int[] iArray){
        int sum = 0;
        for(int i = 0;i<iArray.length;i++){
            if((iArray[i] & 2) > 0 || ((iArray[i] & 4) > 0) || ((iArray[i] & 8) > 0)){
                sum += iArray[i];
            }
        }
        return sum;
    }

    public static double getAverage(int arr[]){
        return 1.0 * getTotal(arr)/arr.length;
    }

    public static void main(String args[]){
        Scanner scan = new Scanner(System.in);
        int size;
        System.out.print("Enter size of array: ");
        size = scan.nextInt();

        int arr[] = new int[size];
        System.out.println("Enter "+size+" integers for array");
        for(int i = 0;i<size;i++){
            arr[i] = scan.nextInt();
        }
        System.out.println();
        System.out.println("Sum = " + getTotal(arr));
        System.out.println("Average = " + getAverage(arr));
        System.out.println("Minimum = " + getLowest(arr));
        System.out.println("Maximum = " + getHighest(arr));
    }
}


Related Solutions

Write a program that asks the user to enter an array of 8 characters then you...
Write a program that asks the user to enter an array of 8 characters then you have to check if characters ‘b’ or ‘a’ appears within the characters and replace them with ‘B’ or ‘A’. For example you enter “a m a l a a b d” The output should be “A m A l A A B d”
Coding in C++: For this verse, you need to write a program that asks the user...
Coding in C++: For this verse, you need to write a program that asks the user to input the inventory id number (integer) and the price (float) for 5 inventory items. Once the 5 inventory items are input from the user, output the results to the screen. Please ensure you use meaningful names for your variables. If your variables are not named meaningfully, points will be deducted.
Write a program that asks the user to enter an array of random numbers, then sort...
Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization. (c++ ) (using while and do while loops)
in C++, Write a program that asks the user to enter 6 numbers. Use an array...
in C++, Write a program that asks the user to enter 6 numbers. Use an array to store these numbers. Your program should then count the number of odd numbers, the number of even numbers, the negative, and positive numbers. At the end, your program should display all of these counts. Remember that 0 is neither negative or positive, so if a zero is entered it should not be counted as positive or negative. However, 0 is an even number....
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Write a program that asks the user for the lengths of the sides of a rectangle....
Write a program that asks the user for the lengths of the sides of a rectangle. Again, check for valid input and exit with an error msg if you don’t get it. Testing: use some known values to confirm that the calculations are correct. E.g. 3 – 4 - 5 triangle >> 3 X 4 rectangle Then print • The area and perimeter of the rectangle • The length of the diagonal (use the Pythagorean theorem). This question should be...
This is Java In this problem we will write a program that asks the user to...
This is Java In this problem we will write a program that asks the user to enter a) The user's first name and b) a series of integers separated by commas. The integers may not include decimal points nor commas. The full string of numbers and commas will not include spaces either, just digits and commas. An example of valid input string is:        7,9,10,2,18,6 The string must be input by the user all at once; do not use a loop...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT