answersLogoWhite

0


Best Answer

Implement these methods:

public static int smallest(int[] arr) {

int small = arr[0];

for(int i = 1; i < arr.size(); i++)

if(arr[i] < small)

small = arr[i];

return small;

}

public static int largest(int[] arr) {

int large = arr[0];

for(int i = 1; i < arr.size(); i++)

if(arr[i] > large)

large = arr[i];

return large;

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What a java program to find largest and smallest value store in the array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What a C function that will compute the K in largest and K in smallest element along with their positions in an array of N signed integers. Assume the following 5 N 50 and 1K?

To compute the largest value in an array, assume that the first element is the largest and store the value. Then traverse the remainder of the array. Each time a larger value is encountered, update the stored value. Once all values are traversed, return the stored value. In pseudocode, this algorithm would be implemented as follows: Algorithm: largest Input: array A of length N Output: largest value in A let largest = A[0] // store first value for index = 1 to N-1 // traverse remaining elements if A[index] &gt; largest then largest = A[index] // update stored value if current value is larger next index return largest To determine the position of the largest value, we alter the algorithm as follows: Algorithm: largest_by_index Input: array A of length N Output: index of the largest value in A let largest = 0; // store index 0 for index = 1 to N-1 // traverse remaining elements if A[index] &gt; A[largest] then largest = index // update stored index next index return largest We can do the same to find the position of the smallest element: Algorithm: smallest_by_index Input: array A of length N Output: index of the smallest value in A let smallest = 0; // store index 0 for index = 1 to N-1 // traverse remaining elements if A[index] &lt; A[smallest] then smallest = index // update stored index next index return smallest To perform both algorithms simultaneously, we need to return two values. To achieve this we can use a simple data structure known as a pair: struct pair { int smallest; int largest; }; Algorithm: range_by_index Input: array A of length N Output: a pair indicating the position of the smallest and largest values in A pair result = {0, 0} // initialise the pair for index = 1 to N-1 // traverse remaining elements if A[index] &lt; A[result.smallest] then result.smallest = index // update stored index else if A[index] &gt; A[result.largest] then result.largest = index // update stored index next index return result


Find the 2nd largest and 2nd smallest from a set of a numbers using 8085 microprocessor?

Arrange the any one of the order and store the memory in order vice (ie.Ascending for Descending) Then print the second data of that array it is simple way


How do you find the minimum value of an array in c language?

Your best bet would probably be to iterate through the array using a for loop and compare each value to the current low and high values (which you would store in a local variable) for example: for each element in array { if current is less than lowest_value lowest_value = current else if current is greater than highest_value highest_value = current }


How to write a program to store ten elements to an array and display the smallest element using C programming?

You data has to be stored in the array arr[] of size 10.double min(const arr[], int arrSize){double minimum = arr[0];for (int j = 0; j < arrSize; j++){if (minimum > arr[j]){minimum = arr[j];}}return minimum;}


Write a java program to print the result in the series?

If you have the series stored in an array, you loop through the array and print each array element in turn. Another possibility is to print out the numbers in the series as you generate them. In that case, you may not need to store anything (depending on the series, of course).

Related questions

What a C function that will compute the K in largest and K in smallest element along with their positions in an array of N signed integers. Assume the following 5 N 50 and 1K?

To compute the largest value in an array, assume that the first element is the largest and store the value. Then traverse the remainder of the array. Each time a larger value is encountered, update the stored value. Once all values are traversed, return the stored value. In pseudocode, this algorithm would be implemented as follows: Algorithm: largest Input: array A of length N Output: largest value in A let largest = A[0] // store first value for index = 1 to N-1 // traverse remaining elements if A[index] &gt; largest then largest = A[index] // update stored value if current value is larger next index return largest To determine the position of the largest value, we alter the algorithm as follows: Algorithm: largest_by_index Input: array A of length N Output: index of the largest value in A let largest = 0; // store index 0 for index = 1 to N-1 // traverse remaining elements if A[index] &gt; A[largest] then largest = index // update stored index next index return largest We can do the same to find the position of the smallest element: Algorithm: smallest_by_index Input: array A of length N Output: index of the smallest value in A let smallest = 0; // store index 0 for index = 1 to N-1 // traverse remaining elements if A[index] &lt; A[smallest] then smallest = index // update stored index next index return smallest To perform both algorithms simultaneously, we need to return two values. To achieve this we can use a simple data structure known as a pair: struct pair { int smallest; int largest; }; Algorithm: range_by_index Input: array A of length N Output: a pair indicating the position of the smallest and largest values in A pair result = {0, 0} // initialise the pair for index = 1 to N-1 // traverse remaining elements if A[index] &lt; A[result.smallest] then result.smallest = index // update stored index else if A[index] &gt; A[result.largest] then result.largest = index // update stored index next index return result


How do you write a programme in c language using arrays to store a ten integer data and find the second smallest and second largest elements in the array?

void mail ( ); { int a, b c = a+b; printf ("%d",=c); }


Find the 2nd largest and 2nd smallest from a set of a numbers using 8085 microprocessor?

Arrange the any one of the order and store the memory in order vice (ie.Ascending for Descending) Then print the second data of that array it is simple way


A travel agent wants a program to store an alphabetical list of winter holiday destinations State the most efficient way to store lists using a programming language?

The most efficient way to store a list is with an array.


How do you find the minimum value of an array in c language?

Your best bet would probably be to iterate through the array using a for loop and compare each value to the current low and high values (which you would store in a local variable) for example: for each element in array { if current is less than lowest_value lowest_value = current else if current is greater than highest_value highest_value = current }


How to write a program to store ten elements to an array and display the smallest element using C programming?

You data has to be stored in the array arr[] of size 10.double min(const arr[], int arrSize){double minimum = arr[0];for (int j = 0; j < arrSize; j++){if (minimum > arr[j]){minimum = arr[j];}}return minimum;}


How do you find c program the maximum and minimum values from a given two dimension array?

To calculate the maximum value in an (unsorted) array, assume the first element is the largest element and store its value. If the next value is larger, store that value, otherwise continue to the next value. Repeat this process, updating the stored value each time you find a larger value. After a single pass of the array, the stored value holds the largest value. Locating the minimum is exactly the same except you're looking for values less than the stored value. The number of dimensions is immaterial. Simply treat the array as if it were one-dimensional. The following example assumes all arrays or of type int: int largest (int* a, unsigned len) { int m = a[0]; for (unsigned i=1; i&lt;len; ++i) if (a[i]&gt;m) m=a[i]; return m; } int smallest (int* a, unsigned len) { int m = a[0]; for (unsigned i=1; i&lt;len; ++i) if (a[i]&lt;m) min=a[i]; return m; } Given these functions, we can determine the largest or smallest value in any array of type int, regardless of the number of dimensions: int main () { int min, max; int a[5] = {4, 2, 5, 3, 1}; min = smallest (a, 5); // min = 1 max = largest (a, 5); // max = 5 int b[3][4] = {{1, 2, 3, 4}, {5, 4, 3, 2}, {0, 1, 2, 3}}; min = smallest ((int*) b, 3*4); // min = 0 max = largest ((int*) b, 3*4); // max = 5 return 0; } Note that the only real complication is that you must cast all multi-dimensional arrays to one-dimensional arrays. That is, for each dimension greater than 1, you add one level of indirection. E.g., a three-dimensional array of type int would be cast to int** while a four-dimensional array casts to int***.


Write a java program to print the result in the series?

If you have the series stored in an array, you loop through the array and print each array element in turn. Another possibility is to print out the numbers in the series as you generate them. In that case, you may not need to store anything (depending on the series, of course).


How do you write a java method to find the second largest integer in an array by using only one loop?

Use two variables to store the largest, and the second-largest integer. Update those in a loop, for every element in the array. Initial values might be the lowest permissible value for the int, long, double, or whatever value you use.


How To find a maximum number in a 2D array?

// Pseudocode int findMax( int[][] data ) { // Return if data is empty if( data.length 0 ) { return 0; } int max = data[0][0]; // Iterate through each element in the array for( int r = 0; r &lt; data.length; ++r ) { for( int c = 0; c &lt; data[0].length; ++c ) { // If we find a value greater than the current max, update max if( data[r][c] &gt; max ) { max = data[r][c]; } } } return max; }


How can you store Class properties in Array in c?

Yes, you can create array that will store class properties. But all of them have to be of the same type.


How can write a c program to store students record in a file?

Write a console based C++ program that reads student information from a text file, build an array of objects of type class StudentInfo,