Advertisements
Advertisements
प्रश्न
Write a program to accept a double dimensional array and find the smallest element along with its location.
कोड लेखन
Advertisements
उत्तर
import java.util.*;
public class DDA1
{
void main()
{
Scanner sc = new Scanner(System.in);
int ar[ ][ ] = new int[3][4];
int r, c;
System.out.println("Fill the array: ");
for(r = 0; r < 3; r++)
{
for(c = 0; c < 4; c++)
{
System.out.print("Cell[" + r + "][" + c + "]: ");
ar[r][c] = sc.nextInt();
}
}
System.out.println("The array is : ");
for(r = 0; r < 3; r++)
{
for(c = 0; c < 4; c++)
{
System.out.print(ar[r][c] + " ");
}
System.out.println();
}
int sm = ar[0][0], pr = 0, pc = 0;
for(r = 0; r < 3; r++)
{
for(c = 0; c < 4; c++)
{
if(sm > ar[r][c])
{
sm = ar[r][c];
pr = r;
pc = c;
}
}
}
System.out.print("Smallest is " + sm + " at [" + pr + "][" + pc + "]");
}
}
Output:
Fill the array:
Cell[0][0]: 50
Cell[0][1]:60
Cell[0][2]:40
Cell[0][3]:80
Cell[1][0]: 42
Cell[1][1]:90
Cell[1][2]:31
Cell[1][3]:21
Cell[2][0]: 78
Cell[2][1]:54
Cell[2][2]: 65
Cell[2][3] : 12
The array is :
50 60 40 80
42 90 31 21
78 54 65 12
Smallest is 12 at [2][3]
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
