When analyzing data sets, such as data for human heights or for human weights, a common step is to adjust the data. This can be done by normalizing to values between 0 and 1, or throwing away outliers. Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, adjust each integer in the list by subtracting the smallest value from all the integers.

Respuesta :

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!

Ver imagen stigawithfun

Following are the java program to calculate the list value:

Program Explanation:

  • Import package.
  • Defining a class Main.
  • Defining a main method.
  • Inside the main method three integer variable "i,x, and m" and an array "userValues" is defined.
  • After defining a variable a Scanner class object is created that inputs "n" and use loop to input array value.
  • By input value a "m" variable is define that holds first element value of array, and use loop that checks first index value greater than array value, and holds its value.
  • At the last step, a loop is defined that subtracts array value with m and prints its value.

Program:

import java.util.*;//import pacakge for user input

public class Main //defining a class  

{

   public static void main(String[] ar)//main method  

   {

       int i,x,m;//defining integer variable

       int userValues[] = new int[20];//defining array

       Scanner obcd = new Scanner(System.in);//creating Scanner class object

       x = obcd.nextInt();//input value in x for input array value  

       if(x>0)//use if that checks x value greater than 0  

       {

           for (i = 0; i < x; i++) //defining a for loop that inputs array value

           {

               userValues[i] = obcd.nextInt();//holding value in array

           }

           m = userValues[0];//defining m variable that hold first element value  

           for (i = 1; i < x; i++) //defining loop that checks array value

           {

               if(m> userValues[i])//use if that checks first index value greater than array value

               {

                   m= userValues[i];//holing array value in m variable

               }

           }

           for (i = 0; i <x; i++) //defining loop that subtracts array value

           {

               System.out.print(userValues[i]-m+" ");//subtracts array value

           }

           System.out.println();

       }

   }

}

Output:

Please find the attached file.

Learn more:

brainly.com/question/13829436

Ver imagen codiepienagoya