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.

Results 1 to 4 of 4

Thread: Java code to find the smallest number from a two dimensional array

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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..


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java code to find the smallest number from a two dimensional array

    Quote Originally Posted by pbrockway2 View Post
    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........

    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...........
    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
    Last edited by pbrockway2; June 23rd, 2013 at 06:40 PM. Reason: code tags added

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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.

Similar Threads

  1. How to find where the biggest number is in an array
    By Jazz26 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 2nd, 2013, 06:07 AM
  2. 3x3 array find largest number
    By joecan2010 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 27th, 2012, 11:56 PM
  3. [SOLVED] how to find all occurrences of a number in an array recursivelly
    By mia_tech in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 13th, 2012, 01:37 PM
  4. Find Biggest Number in Array
    By jo15765 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 12th, 2012, 03:01 PM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM

Tags for this Thread