Hi,
i need the code to find the smallest number in a two dimensional array.
pls help me.....
Thanks..
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi,
i need the code to find the smallest number in a two dimensional array.
pls help me.....
Thanks..
What ideas have you got? In other words if you were given a piece of paper with a matrix of numbers on it, how would you (rather than a computer) go about finding the smallest number? In particular you would have to be careful not to leave any numbers out as you went about checking the array.
What code have you got? That is, it would help if you posed the code for a class with a main() method which declares and populates the array.
Can you write code for anything similar but simpler? For instance code to find the smallest number from a simple array.
Thanks Sir
giving the code...
--- Update ---
here is the code to take input for two dimensional array
and displaying it........
now in this code i want to add a module to find the minimum of the given array...........package Array; import java.util.Scanner; public class twodimensional_array { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); int rows; int columns; System.out.println("Enter number of rows: "); rows = scanner.nextInt(); System.out.println ("Now enter the number of columns: "); columns = scanner.nextInt(); int [][] matrix = new int [rows] [columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { System.out.println("Enter the number for row " + i + " and column " + j + " : "); matrix [i][j] = scanner.nextInt(); //////////////////////////////////////////////////////////// /* System.out.println("Matrix A: "); for (int i1 = 0; i1 < matrix .length; i1++) { System.out.println(); for (int j1 = 0; j1 < matrix [i].length; j1++) { System.out.print(matrix [i1][j1] + " "); } }*/ ////// } } /** * @author Prabhat * @return displaying the array list */ System.out.println("Matrix A: "); for (int i1 = 0; i1 < matrix .length; i1++) { System.out.println(); for (int j1 = 0; j1 < matrix [i1].length; j1++) { System.out.print(matrix [i1][j1] + " "); } } } }
--- Update ---
Hi
got the problem resolved.........
giving the code below...........
Thanks to allpackage Array; import java.util.Scanner; public class twodimensional_array { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); int rows; int columns; System.out.println("Enter number of rows: "); rows = scanner.nextInt(); System.out.println ("Now enter the number of columns: "); columns = scanner.nextInt(); int [][] matrix = new int [rows] [columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { System.out.println("Enter the number for row " + i + " and column " + j + " : "); matrix [i][j] = scanner.nextInt(); //////////////////////////////////////////////////////////// /* System.out.println("Matrix A: "); for (int i1 = 0; i1 < matrix .length; i1++) { System.out.println(); for (int j1 = 0; j1 < matrix [i].length; j1++) { System.out.print(matrix [i1][j1] + " "); } }*/ ////// } } /** * @author Prabhat * @return displaying the array list */ System.out.println("Matrix A: "); for (int i1 = 0; i1 < matrix .length; i1++) { System.out.println(); for (int j1 = 0; j1 < matrix [i1].length; j1++) { System.out.print(matrix [i1][j1] + " "); } } ///******** Ending the displaying module***************//// ///*********starting the displaying the max vlaue of given array***************//// /** * @author Prabhat * @return displaying the smaallest value of given array */ int maxValue = Integer.MIN_VALUE; System.out.println("\nMax values in 2D array: "); for (int i2 = 0; i2 < matrix.length; i2++) for (int j2 = 0; j2 < matrix[i2].length; j2++) if (matrix[i2][j2] > maxValue) maxValue = matrix[i2][j2]; System.out.println("Maximum value: " + maxValue); ///*****Ending the Min value of given array******************************/////////// //******Finding the Min value of array****************************//// int minValue =Integer.MAX_VALUE; System.out.println("\nMin values in 2D array: "); for (int i2 = 0; i2 < matrix.length; i2++) for (int j2 = 0; j2 < matrix[i2].length; j2++) if (matrix[i2][j2] < minValue) minValue = matrix[i2][j2]; System.out.println("Minimum value: " + minValue); //**** End of Finding the max value of an array*****************///// } }
Last edited by pbrockway2; June 23rd, 2013 at 07:40 PM. Reason: code tags added
Well done! Sorry I haven't been able to log in until now...
You are right: a common way to find the maximum value is:
- choose a really small value as the maximum
- for every element
- if it is bigger make it the new maximum
- the maximum is now the maximum of all the elements
The tricky bit (for any data structure) is going through *every* element. For a 2d array you do that with a double loop - and your code shows how the same double loop that you used for setting (and printing) the array values is also effective for finding the maximum.