Share on Google+Share on Google+

Determine Average by removing Largest and Smallest Number

Advertisement
In this section, you will learn how to remove largest and smallest number from the 10 numbers and then determine the average.

Determine Average by removing Largest and Smallest Number

In this section, you will learn how to remove largest and smallest number from the 10 numbers and then determine the average. For this, an user is allowed to input 10 numbers. These numbers are then stored into an array in order to find the largest and smallest number. Now to remove these highest and lowest numbers from the array, we have created a method deleteFromArray().

public static int[] deleteFromArray(int index, int[] arr, int value) {
		boolean[] deleteNumber = new boolean[arr.length];
		int size = 0;
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] == value) {
				deleteNumber[i] = true;
			} else {
				deleteNumber[i] = false;
				size++;
			}
		}
		int[] newArr = new int[size];
		int in = 0;
		for (int i = 0; i < arr.length; i++) {
			if (!deleteNumber[i]) {
				newArr[in++] = arr[i];
			}
		}
		return newArr;
	}

After that, we have got the final array of 8 numbers. The sum of all these numbers are stored into a variable 'sum' which is further used in determining the average.

Here is the code:

import java.util.*;

class Calculate {
	public static int[] deleteFromArray(int index, int[] arr, int value) {
		boolean[] deleteNumber = new boolean[arr.length];
		int size = 0;
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] == value) {
				deleteNumber[i] = true;
			} else {
				deleteNumber[i] = false;
				size++;
			}
		}
		int[] newArr = new int[size];
		int in = 0;
		for (int i = 0; i < arr.length; i++) {
			if (!deleteNumber[i]) {
				newArr[in++] = arr[i];
			}
		}
		return newArr;
	}

	public static void main(String[] args) {
		int sum = 0;
		int arr[] = new int[10];
		int max = Integer.MIN_VALUE;
		int min = Integer.MAX_VALUE;
		int j = 0, k = 0;

		Scanner input = new Scanner(System.in);
		System.out.println("Enter 10 numbers:");
		for (int i = 0; i < arr.length; i++) {
			arr[i] = input.nextInt();
			if (arr[i] < min) {
				min = arr[i];
				j = i;
			}
			if (arr[i] > max) {
				max = arr[i];
				k = i;
			}
		}
		System.out.println("Minimum=" + min);
		System.out.println("Maximun=" + max);
		int newArr[] = Calculate.deleteFromArray(j, arr, min);
		int finalArr[] = Calculate.deleteFromArray(k, newArr, max);
		for (int i = 0; i < finalArr.length; i++) {
			sum += finalArr[i];
		}
		System.out.println("Final Score: " + sum);
		int average = sum / 8;
		System.out.println("Average Score: " + average);

	}

}

Output:

Enter 10 numbers:
2
3
1
4
9
5
6
7
10
8
Minimum=1
Maximum=10
Final Score: 44
Average Score: 5

Advertisements

Posted on: January 15, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.