+ 1

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
17 Réponses
+ 5
Arnav , what Ausgrindtube said. And also, it should be <4, or <=3, because you have a 4x4 matrix 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} }; /* problematic */ //int small = 0; //int large = 0; /* use Shardis Wolfe's suggestion instead: */ int small = a [0][0]; int large = a[0][0]; for(int i =0; i<4; i++) { for(int j =0; j<4; 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); } }
24th Jan 2025, 2:35 AM
Bob_Li
Bob_Li - avatar
+ 5
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
+ 5
Arnav Bob_Li shows a better version of your sample code. The key difference is initializing the small and large values outside of the for loops, and then ONLY updating them inside the for loops IF the value at your current i and j is smaller or larger than current small or large. One correction to both yours and Bob's code would be initializing both small and large to a[0][0] instead of 0. There is no guarantee that 0 is a value in the 2D array or that there will always be values smaller and larger than 0.
24th Jan 2025, 4:59 AM
Shardis Wolfe
+ 4
or use a nested for-each loop, which have an advantage of being able to work with jagged arrays. https://sololearn.com/compiler-playground/cIOFbaK2M14e/?ref=app
24th Jan 2025, 2:55 AM
Bob_Li
Bob_Li - avatar
23rd Jan 2025, 12:48 PM
Ausgrindtube
Ausgrindtube - avatar
M
+ 2
Shardis Wolfe you're right. using a[0][0] is the correct way.
24th Jan 2025, 5:58 AM
Bob_Li
Bob_Li - avatar
+ 2
Aight thanks everyone.I get it now
24th Jan 2025, 7:39 AM
Arnav
Arnav - avatar
+ 1
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
+ 1
#include <iostream> #include <string> using namespace std; class Messenger { public: string username; string message; ```void sendMessage() { cout << username << ": " << message << endl; } void receiveMessage(string msg) { cout << "تم استلام الرسالة: " << msg << endl; } ``` }; int main() { Messenger messenger; string username, message; ```cout << "أدخل اسم المستخدم: "; getline(cin, username); messenger.username = username; while (true) { cout << "أدخل الرسالة (أو 'خروج' للخروج): "; getline(cin, message); if (message == "خروج") { break; } Vu messenger.sendMessage(); } return 0; ``` }
25th Jan 2025, 12:08 AM
yousf ferjani
yousf ferjani - avatar