Question

In: Computer Science

JAVA 6.8 PRACTICE: Loops*: Biggest difference Write a program that outputs the biggest difference (absolute value)...

JAVA 6.8 PRACTICE: Loops*: Biggest difference Write a program that outputs the biggest difference (absolute value) between any successive pair of numbers in a list. Such a list might represent daily stock market prices or daily temperatures, so the difference represents the biggest single-day change. The input is the list size, followed by the numbers. If the input is 5 60 63 68 61 59, the output is 7. Hints: Declare a variable for the current number, and another for the previous number. At the start of each loop iteration, set prevNum = currNum, then get the next number into currNum. Maintain a max difference variable. Initialize it with 0. In each loop iteration, check if the difference between currNum and prevNum exceeds maxDiff; if so, update maxDiff with that difference. Don't forget to take the absolute value of the difference before the above comparison. Don't try to check the max difference for the first number in the list, since no previous number exists.

Solutions

Expert Solution

JAVA PROGRAM:

import java.util.*;

public class Test {

     // main() function

     public static void main(String[] args) {

        // Declare Scanner object sc

        Scanner sc = new Scanner(System.in);

        //Declare a variable for the current number, previous number

        int currNum,prevNum;

        // Initialize it with 0

        int maxDiff = 0;

        // Ask the input size of list

        System.out.print("Enter input is: ");

        int list_size = sc.nextInt();

        // Declare the list

        int[] list = new int[list_size];

        // Ask the user for list element

        for(int i=0;i<list_size;i++){

            list[i] =sc.nextInt();

        }

        // for loop iteration,

        for(int i=0;i<list_size-1;i++){

            // set prevNum = currNum

            prevNum = list[i];

            //get the next number into currNum

            currNum = list[i+1];

            // check if the difference (currNum - prevNum) exceeds maxDiff

            // take absolute value of the difference using Math.abs() function

            if(Math.abs(currNum-prevNum)>maxDiff){

                 //update maxDiff with that difference

                 maxDiff = Math.abs(currNum-prevNum);

            }

        }

        // Display the output maxdiff

        System.out.println("The output is: "+ maxDiff);

     }

}

OUTPUT:

NOTE: If you don't understand any step please tell me.


Related Solutions

Write a program in java which is in main and uses no classes, methods, loops or...
Write a program in java which is in main and uses no classes, methods, loops or if statements Ask the user to enter their first name Ask the user to enter their last name Print their initials followed by periods (ie. Clark Kent = C. K.) Print the number of letters in their last name
Write a program in Java that reads in a set of positive integers and outputs how...
Write a program in Java that reads in a set of positive integers and outputs how many times a particular number appears in the list. You may assume that the data set has at most 100 numbers and -999 marks the end of the input data. The numbers must be output in increasing order. For example, for the data 15 40 28 62 95 15 28 13 62 65 48 95 65 62 65 95 95 -999 The output is...
Write a Java program that uses nested for loops to print a multiplication table as shown...
Write a Java program that uses nested for loops to print a multiplication table as shown below.   Make sure to include the table headings and separators as shown.   The values in the body of the table should be computed using the values in the heading   e.g. row 1 column 3 is 1 times 3.
write a java code program using loops to compute the sum of the 30 terms of...
write a java code program using loops to compute the sum of the 30 terms of the series below. Find sum of all integers between 100 and 200 which are divisible by 9 or 13 Write a program to find the sum of the first 8 terms of the series 1 + 11 + 111 + 1111 + . . . Output: Term Ratio Sum
Write a Java program for RSA encryption that has the following inputs and outputs: Given a...
Write a Java program for RSA encryption that has the following inputs and outputs: Given a message and an integer n = pq where p and q are odd primes and an integer e > 1 relatively prime to (p − 1)(q − 1), encrypt the message using the RSA cryptosystem with key (n, e).
Write a Java program that prompts a user for 10 integers. When completed it outputs the...
Write a Java program that prompts a user for 10 integers. When completed it outputs the highest number, the lowest number, the number of odd number, and the average of the numbers
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
In this exercise we will practice using nested loops. You will write s program that prompts...
In this exercise we will practice using nested loops. You will write s program that prompts the user to enter an integer between 1 and 9 and displays a pyramid of numbers, as shown in the example below. The program must validate the input given by the user and make sure that: if the user enters a non-integer the program issues an error message and requests the user provides a valid input. if the user does not enter a number...
Write a java program that will ask the user to enter integers (use loops) until -100...
Write a java program that will ask the user to enter integers (use loops) until -100 is entered. The program will find and display the greatest, the smallest, the sum and the average of the entered numbers. also write a separate driver class which will be used to run and display the greatest, the smallest, the sum and the average of the entered numbers. Thanks!!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT