Answer:
import java.util.Scanner;
public class Normalization {
public static void main(String[] args) {
//Create an object of the Scanner class to allow for user's inputs
Scanner input = new Scanner(System.in);
// 1. Create a prompt for the user to enter the list of integers
System.out.println("Enter the list of integers separated by space : ");
// 2. Get the first integer being input by user and store in an
// integer variable, fnumber. The first integer determines the
//length of the list
int fnumber = input.nextInt();
// 3. Create an array of integers to hold the other elements in the
// list. The array has a size denoted by the first number (stored in
// fnumber) in the user's input.
int[] list = new int[fnumber];
// 4. Create a while loop to populate the array with the rest of the
// user's input.
int j = 0;
while (j < fnumber) {
list[j] = input.nextInt();
j++;
}
// 5. Create another loop to print out the elements in the list.
// This is an optional step. This is just to check the elements in the
// list before normalization.
System.out.println("List before normalization");
for (int i = 0; i < list.length; i++) {
System.out.print(list[i] + " ");
}
// Print a new line after printing the list. This is just for formatting
// purpose.
System.out.println();
// 6. Find the smallest element in the list.
// First get the first element in the list and store it in a variable
// called smallest. This is to tentatively assume that the first
// element in the list is the smallest
.
int smallest = list[0];
// Loop through the list to check if there is any element smaller
// than the assumed element.
// If there is, replace the assumed element with such.
for (int i = 1; i < list.length; i++) {
if (list[i] < smallest) {
smallest = list[i];
}
}
// 7. Print out the smallest element. This is an optional step. This is
// just to be double check if the program actually got the smallest
// number in the list
System.out.println("Smallest element : " + smallest);
// 8. Loop through the list once again.
// At every cycle, deduct the smallest number, gotten above, from
// each list element.
for (int i = 0; i < list.length; i++) {
list[i] = list[i] - smallest;
}
// 9. Print out the resulting elements in the list
System.out.println("List after normalization");
for (int i = 0; i < list.length; i++) {
System.out.print(list[i] + " ");
}
}
}
Explanation:
The source code file for this program has also been attached to this response. Kindly download it and go through the comments in the code to get a better understanding of the code. The comments explain every segment of the code. Using comments is a better way of explaining every line of the code.
Hope this helps!