Answer to Question #279405 in Java | JSP | JSF for jatin

Question #279405

Write statements that perform the following one-dimensional-array operations (To test the items a-e below, create an integer array called numbers with 10 elements. Array should be first pre-initialized with random numbers from 1-100) or You can create a user defined Array taking input from user

For each array element.

 

a) Crete a method that will return sum of all the elements of an integer array.

b) Crete a method that will return the highest number in the array.  

c)Crete a method that will return the lowest number in array.

d) Display all the values of an array in column format.

e) Create a method that will reverse the array or create a method to get the Average of the array. 


1
Expert's answer
2021-12-14T01:15:20-0500
import java.util.Random;

public class Main {
    public static int sum(int[] array) {
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        return sum;
    }

    public static int max(int[] array) {
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < array.length; i++) {
            max = Math.max(max, array[i]);
        }
        return max;
    }

    public static int min(int[] array) {
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < array.length; i++) {
            min = Math.min(min, array[i]);
        }
        return min;
    }

    public static void reverse(int[] array) {
        for (int i = 0, j = array.length - 1; i < array.length / 2; i++, j--) {
            int tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
        }
    }

    public static void print(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

    public static void main(String[] args) {
        int[] array = new int[10];
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(100) + 1;
        }
        System.out.println("Sum: " + sum(array));
        System.out.println("Max: " + max(array));
        System.out.println("Min: " + min(array));
        print(array);
        System.out.println();
        reverse(array);
        print(array);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog