There are many ways to do this. One way is to import "java.util.Arrays;." Then you can make your array of numbers. For example int[] test = {4, 123, 432, 314}; Then later on in the code you can use Arrays.sort(test); That will sort it from min to max. Then to output it. System.out.println(test[0] + " " + test[length-1]);
final double[] ns = new double[10]; final Random rnd = new Random(System.currentTimeMillis()); // Fill... for (int i = 0; i < ns.length; ++i) { ns[i] = rnd.nextDouble(); } // Get largest/smallest... double largest = Double.MIN_VALUE; double smallest = Double.MAX_VALUE; for (double n : ns) { if (n > largest) { largest = n; } if (n < smallest) { smallest = n; } } // largest and smallest are now the proper values.
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] > 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] > 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] < 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] < A[result.smallest] then result.smallest = index // update stored index else if A[index] > A[result.largest] then result.largest = index // update stored index next index return result
// 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 < data.length; ++r ) { for( int c = 0; c < data[0].length; ++c ) { // If we find a value greater than the current max, update max if( data[r][c] > max ) { max = data[r][c]; } } } return max; }
1. Read the 3 nos a,b,c 2. Let larget = a 3. if b > largest then largest = b 4. if c > largest then largest = c..... If you have to process more nos, read all of them in an array. Assume the first element be largest, do comparison through all elements of the array.... Similar algorithm can be developed for finding the lowest also. /*lab practice 2 damithguruge question 4 */ #include<stdio.h> int main() { int num1,num2,num3,num4; int smallest; printf("Please enter a number1"); scanf("%d%*c", &num1); printf("Please enter a number2"); scanf("%d%*c" ,&num2); printf("Please enter a number3"); scanf("%d%*c", &num3); Printf("Please enter a numbe4r"); scanf("%d%*c", &num4); /* num1 set as the smallest */ smallest=num1; if(smallest > num2) then smallest=num2; else if smallest >num3 then smallest=num3; else if smallest>num4 then smallest=num4; printf("smallest number:%d\n,smallest"); return(0); endif endif endif }
Please rephrase your question. An array usually has a fixed size and I don't recall ever having to "go below its size". This implies that the missing elements are not within the range of the array.
There is no such condition. The algorithm to locate the second largest number in an array of numbers is: Sort the array in descending order. Remove the duplicate values from the array. If there are two or more elements remaining, return the second number. If there is less than two elements remaining, return the NaN value (not a number).
final double[] ns = new double[10]; final Random rnd = new Random(System.currentTimeMillis()); // Fill... for (int i = 0; i < ns.length; ++i) { ns[i] = rnd.nextDouble(); } // Get largest/smallest... double largest = Double.MIN_VALUE; double smallest = Double.MAX_VALUE; for (double n : ns) { if (n > largest) { largest = n; } if (n < smallest) { smallest = n; } } // largest and smallest are now the proper values.
Identifying array-elements.
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] > 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] > 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] < 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] < A[result.smallest] then result.smallest = index // update stored index else if A[index] > A[result.largest] then result.largest = index // update stored index next index return result
// 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 < data.length; ++r ) { for( int c = 0; c < data[0].length; ++c ) { // If we find a value greater than the current max, update max if( data[r][c] > max ) { max = data[r][c]; } } } return max; }
1. Read the 3 nos a,b,c 2. Let larget = a 3. if b > largest then largest = b 4. if c > largest then largest = c..... If you have to process more nos, read all of them in an array. Assume the first element be largest, do comparison through all elements of the array.... Similar algorithm can be developed for finding the lowest also. /*lab practice 2 damithguruge question 4 */ #include<stdio.h> int main() { int num1,num2,num3,num4; int smallest; printf("Please enter a number1"); scanf("%d%*c", &num1); printf("Please enter a number2"); scanf("%d%*c" ,&num2); printf("Please enter a number3"); scanf("%d%*c", &num3); Printf("Please enter a numbe4r"); scanf("%d%*c", &num4); /* num1 set as the smallest */ smallest=num1; if(smallest > num2) then smallest=num2; else if smallest >num3 then smallest=num3; else if smallest>num4 then smallest=num4; printf("smallest number:%d\n,smallest"); return(0); endif endif endif }
Method 1: Sort the array in descending order, compare 1st and 2nd if not same , return 2nd if same return -1 Method 2: Find the largest number in the array, initialize another array with dimension 1 less than of original. Copy the array elements from the original array minus the largest element. not select largest from the second array and compare with the previous one if not same return the second largest if same return -1
void mail ( ); { int a, b c = a+b; printf ("%d",=c); }
Please rephrase your question. An array usually has a fixed size and I don't recall ever having to "go below its size". This implies that the missing elements are not within the range of the array.
An array is a set of numbers that form some sort of regular arrangement. A linear array is a 1-dimensional array consisting of a row or a column of a set of numbers. A 2-dimensional array is a rectangular arrangement of numbers. And there are arrays with higher dimensions. The elements of an array need not be numbers: they could be variables, functions or expressions. In other words, it's a picture to describe a multiplication problem.
knowledge
#include<stdio.h> void main() { int a[10],n,i,large,small; printf("enter the value of n\n"); scanf("%d",&n); printf("enter the elements\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); large=a[0]; small=a[0]; for(i=1;i<n;i++) { if(a[i]>large) large=a[i]; if(a[i]<small); small=a[i];} printf("largest element in the array id %d\n",large); printf("smallest element in the array is %d\n",small); }