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 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
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!!
Write a guessing game java program (basic program, we didn't study further than loops and files)...
Write a guessing game java program (basic program, we didn't study further than loops and files) where the user has to guess a secret number between 1 and 100. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed. It counts only as one try if they input the same number multiple times consecutively.Example of running this program: Enter your secret number...
Please show screenshot outputs and fully functional code for the Java program. Write the following methods...
Please show screenshot outputs and fully functional code for the Java program. Write the following methods to   1) read the content of an array of 5 doubles public static double[] readingArray() 2) find and print:the smallest element in an array of 5 double public static void smallest(double [] array) 3) find and print:the largest element in an array of 5 doubles pubic static void largest (double [] array) In the main method - invoke readingArray and enter the following numbers...
Create a Java application NumberLoops to practice loops. The draft has one loop to calculate a...
Create a Java application NumberLoops to practice loops. The draft has one loop to calculate a product and the final will add another loop to print integers in a required format. There is no starter code for the problem. Use a Scanner to input an integer then compute and display the product of all positive integers that are less than the input number and multiple of 3. If no numbers satisfy the conditions, then print out 1 for the product....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT