Java code to find the smallest number from a two dimensional array
Hi,
i need the code to find the smallest number in a two dimensional array.
pls help me.....
Thanks..
Re: Java code to find the smallest number from a two dimensional array
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.
Re: Java code to find the smallest number from a two dimensional array
Quote:
Originally Posted by
pbrockway2
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........
Code :
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] + " ");
}
}
}
}
now in this code i want to add a module to find the minimum of the given array...........
--- Update ---
Hi
got the problem resolved.........
giving the code below...........
Code :
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] + " ");
}
}
///******** 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*****************/////
}
}
Thanks to all
Re: Java code to find the smallest number from a two dimensional array
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.