0

Double Dimensional Array largest and smallest element

Hello,I need help with a code (in java) that takes a DDA as input and find the largest and smallest element in the array.Can anyone help me with it.I get problem while comparing the elements and changing the values of the smallest and largest element.

23rd Jan 2025, 12:29 PM
Arnav
Arnav - avatar
3 Respostas
+ 2
Arnav the code logic overwrites small and large with every loop iteration. That makes any previous stored comparisons meaningless. Values of small and large need to be initialized outside of the loops, and then updated inside the loops only when new maxima/minima are found.
23rd Jan 2025, 3:11 PM
Brian
Brian - avatar
23rd Jan 2025, 12:48 PM
Ausgrindtube
Ausgrindtube - avatar
0
public class Program { public static void main(String[] args) { int a[][]={{1,2,3,23}, {8,10,5,3}, {9,56,21,0}, {33,56,77,88}}; int i = 0; int small = 0; int large =0; for( i =0;i<3;i++) { for(int j =0;j<3;j++) { small = a[i][j]; large = a[i][j]; if(a[i][j+1]>large) large =a[i][j+1]; if(a[i][j+1]<small) small =a[i][j+1]; } } System.out.println("Large= "+large); System.out.println("small= "+small); }} Ausgrindtube
23rd Jan 2025, 12:53 PM
Arnav
Arnav - avatar