Computer Applications
int X[][] = {{4, 5}, {7, 2}, {19, 4}, {7, 4}};
Write the index of the maximum element and the index of the minimum element of the array.
Answer
Maximum element index: X[2][0]
Minimum element index: X[1][1]
Explanation
This is a 2D array (matrix) with 4 rows and 2 columns. Each element is accessed using X[row][column].
| Row | Column 0 | Column 1 |
|---|---|---|
| 0 | 4 | 5 |
| 1 | 7 | 2 |
| 2 | 19 | 4 |
| 3 | 7 | 4 |
To find the maximum element, all values in the array are compared. The largest value is 19, which is located in row 2 and column 0. Hence, its index is X[2][0].
To find the minimum element, all values are compared again. The smallest value is 2, which is located in row 1 and column 1. Hence, its index is X[1][1].
Related Questions
Give the output of the following program segment and mention how many times the loop is executed.
int K = 1; do {K += 2; System.out.println(K); } while (K <= 6);The following program segment calculates and displays the factorial of a number. [Example : Factorial of 5 is 1 x 2 x 3 x 4 x 5 = 120]
int p, n = 5, f = 0; for(p = n; p > 0; p--) f *= p; System.out.println(f);Name the type of error if any; correct the statement to get the desired output.
The following program segment swaps the first element and the second element of the given array without using the third variable, fill in the blanks with appropriate java statements:
void swap() { int x[] = {4, 8, 19, 24, 15}; (1) ............... ; (2) ............... ; x[0] = x[0] / x[1]; System.out.println(x[0] + " " + x[1]); }Name the following:
(a) The return data type of the method equals().
(b) The String method which has no parameter and returns a String.