Question

In: Computer Science

Write a Java program to get the difference between the largest and smallest values in a...

Write a Java program to get the difference between the largest and smallest values in a user inputed array of integers. The length of the array must be 1 and above

Solutions

Expert Solution

Solution.

import java.util.Scanner;

classMin_max_array

{

//Steps to get maximum number from array elements

int getMax(int[]inputArray)

{

int maxValue=inputArray[0];

for(int i=1;i<inputArray.length;i++)

{

if(inputArray[i]>maxValue)

{

maxValue=inputArray[i];

}

}

return maxValue;

}

//Steps to get minimum number from array elements

int getMin(int[]inputArray)

{

int minValue=inputArray[0];

for(int i=1;i<inputArray.length;i++)

{

if(inputArray[i]<minValue)

{

minValue=inputArray[i];

}

}

return minValue;

}

}

public class Array_difference

{

public static void main(String[] args)

{

int n;

// creating object of scanner.

Scanner sc = new Scanner(System.in);

//Enter number of array elements here.

System.out.print("Enter number of elements you wants to enter : " );

//Read the entered number and store it in variable "n".

n=sc.nextInt();

int arr[]=new int[n];

for(int i=0;i<arr.length;i++)

{

System.out.print("Enter ["+(i+1)+"] element : " );

arr[i]=sc.nextInt();

}

MinMaxInArray mm=new MinMaxInArray();

//Print maximum, minimum, difference of array elements.

System.out.println("Maximum value is : " +mm.getMax(arr));

System.out.println("Minimum value is : " +mm.getMin(arr));

int Difference=mm.getMax(arr)-mm.getMin(arr);

System.out.print("Difference between Minnimum and Maximum in array is : " +Difference );

}

}

Output

Enter number of elements you wants to enter :10

Enter [1] element : 25

Enter [2] element : 36

Enter [3] element : 74

Enter [4] element : 85

Enter [5] element : 964

Enter [6] element : 215

Enter [7] element : 36

Enter [8] element : 58

Enter [9] element : 98

Enter [10] element : 123

Maximum value is : 964

Minimum value is : 25

Difference between Minnimum and Maximum in array is : 939


Related Solutions

1. Write a program that computes the smallest and largest of a set of values that...
1. Write a program that computes the smallest and largest of a set of values that are stored in an array. Ask the user for the set size (and hence the array size). Populate the array with user input.
1. Write a program that computes the smallest and largest of a set of values that...
1. Write a program that computes the smallest and largest of a set of values that are stored in an array. Ask the user for the set size (and hence the array size). Populate the array with user input. (Java language)
Write a program whose inputs are three integers, and whose outputs are the largest of the three values and the smallest of the three values.
 in Coral Simulator  Write a program whose inputs are three integers, and whose outputs are the largest of the three values and the smallest of the three values. If the input is 7 15 3, the output is: largest: 15 smallest: 3 Your program should define and call two functions: Function LargestNumber(integer num1, integer num2, integer num3) returns integer largestNum Function SmallestNumber(integer num1, integer num2, integer num3) returns integer smallestNum The function LargestNumber should return the largest number of the...
• Write a C++ program to find the largest umber, smallest number and sum of all...
• Write a C++ program to find the largest umber, smallest number and sum of all the element of a given array of 20 integers • Note − Declare array to 20 numbers − Input values for 20 array elements − Find largest number − Find smallest number − Find sum of all numbers • Display the results
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester.    The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Write a Java program for slidsender and slidreiver 1. Start the program 2. Get the frame...
Write a Java program for slidsender and slidreiver 1. Start the program 2. Get the frame size from the user 3. To create the frame based on the user 4. To send frames to server from the client 5. If your frames reach the server it will send ACK signal to client otherwise it will send NACK signal to 6. Stop the program
Write a complete java program to get input of a person’s age and their years of...
Write a complete java program to get input of a person’s age and their years of current USA citizenship. Tell them if they are eligible to run for US House of Representatives, US Senate, or President. At first, have the program just run once and give the answer for the given inputs. Give the answer in a nice format and be clear which offices the person can run for. Write good and complete pseudo code. Next, put that program in...
Write a complete java program to get input of a person’s age and their years of...
Write a complete java program to get input of a person’s age and their years of current USA citizenship. Tell them if they are eligible to run for US House of Representatives, US Senate, or President. At first, have the program just run once and give the answer for the given inputs. Give the answer in a nice format and be clear which offices the person can run for. Write good and complete pseudo code. Next, put that program in...
Let A and B be two events. Find the largest and smallest possible values P(A U...
Let A and B be two events. Find the largest and smallest possible values P(A U B) can take in terms of P(A) and P(B) and give examples in which these values can be attained.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT