Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By SurfMan

Thread: java Handling matrices

  1. #1
    danielki is offline Member
    Join Date
    Feb 2014
    Posts
    16
    Rep Power
    0

    Default java Handling matrices

    Hello,
    Can any one help me why my code is not printing the matrix and sum?
    thank you!
    Assignment
    The Java-program Matrix below first asks the user for the number of rows and columns in a matrix. After this the program asks for the values of the elements. Finally, program prints the elements of the matrix and their sum on screen. Your task is to create the missing methods. Check the example print to see how to modify the print. When printing the matrix, values on the same row are separated using tabulator.

    Program to complete:

    import java.util.Scanner;


    public class Matrix {

    public static void main(String[] args) {

    int rows, columns;

    Scanner reader = new Scanner(System.in);



    System.out.print("Type in the number of rows: ");

    rows = reader.nextInt();

    System.out.print("Type in the number of columns: ");

    columns = reader.nextInt();


    int matrix [][];


    matrix = askInfo(rows, columns);

    printMatrix(matrix);

    countSum(matrix);

    }


    // Write the missing methods here

    // Methods are written in the text box below.

    }

    Example output


    Type in the number of rows: 3
    Type in the number of columns: 4
    Type in the element 1 of the row 1: 1
    Type in the element 2 of the row 1: 2
    Type in the element 3 of the row 1: 3
    Type in the element 4 of the row 1: 4
    Type in the element 1 of the row 2: 5
    Type in the element 2 of the row 2: 6
    Type in the element 3 of the row 2: 7
    Type in the element 4 of the row 2: 8
    Type in the element 1 of the row 3: 9
    Type in the element 2 of the row 3: 10
    Type in the element 3 of the row 3: 11
    Type in the element 4 of the row 3: 12

    Matrix:
    1 2 3 4
    5 6 7 8
    9 10 11 12

    Sum of the elements of the matrix: 78
    my code

    Java Code:
        import java.util.Scanner;  
          
          
        public class apples {  
          
            public static void main(String[] args) {  
          
                int rows, columns;  
          
                Scanner reader = new Scanner(System.in);  
          
                  
          
                System.out.print("Type in the number of rows: ");  
          
                rows = reader.nextInt();  
          
                System.out.print("Type in the number of columns: ");  
          
                columns = reader.nextInt();  
          
          
                int matrix [][];  
          
          
                matrix = askInfo(rows, columns);  
          
                printMatrix(matrix);  
          
                countSum(matrix);  
          
            }  
        // method  
        private static int[][] askInfo(int rows, int columns) {  
            Scanner reader = new Scanner(System.in);  
                int[][] matrix = new int[rows][columns];  
                int sum=0;  
                for (int i = 0; i < rows; i++) {  
                    for (int j = 0; j < columns; j++) {  
                         System.out.print("Type in the element "+(j+1)+" of the row "+(i+1)+": ");  
                        matrix[i][j] = reader.nextInt();  
                        sum += matrix[i][j];  
                         
                          
                    }  
                }return matrix;  
                  
            }  
            
            private static void printMatrix(int[][] matrix) {  
                int rows=0, columns=0;  
                    for (int i = 0; i < rows; i++) {  
                    for (int j = 0; j < columns; j++) {  
                        System.out.print(matrix[i][j] + " ");  
                    }  
                    System.out.print("\n");  
                }  
                
            }  
              
             private static void countSum(int[][] matrix) {  
             int sum=0;  
              System.out.println("Sum of the elements of the matrix: "+sum);  
                
            }  
          
        }

  2. #2
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,002
    Rep Power
    3

    Default Re: java Handling matrices

    On line 51 you set int rows=0, columns=0 so there are no rows and columns to loop over. I would suggest you take the length of the matrix array to loop over instead of a hardcoded row or column.
    danielki likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    danielki is offline Member
    Join Date
    Feb 2014
    Posts
    16
    Rep Power
    0

    Default Re: java Handling matrices

    Hello,
    Thank you for the suggestions. As per the assignment my task is only to create the methods.
    I am not supposed to touch the main method, that is what it makes hard for me. When I modify
    int rows=0; int columns=0; to int rows, columns; it says I have to initialize. So generally I am trying
    to create three methods which fits the main. Is it possible to use length the matrix array inside a private
    method with out modifying the given code in the assignment? thank you for your help!

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: java Handling matrices

    Use the array's .length property to control looping. For example for the num array:
    Java Code:
           for(int i = 0 ; i < num.length ; i++)
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    danielki is offline Member
    Join Date
    Feb 2014
    Posts
    16
    Rep Power
    0

    Default Re: java Handling matrices

    Hi Norm,
    Thanks, when I use the array's length, like for example
    for (int i = 0; i < rows.length; i++) {
    it tells me "The primitive type int of rows does not have a field length"
    And I can't modify any thing on the main, my task is only to create the
    missing 3 methods.
    Thanks

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: java Handling matrices

    Is rows an array? .length is for arrays.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: java Handling matrices

    Rows is not an array, just an int. Do this:

    Java Code:
    for(int i = 0; i < matrix.length; i++)

Similar Threads

  1. Help with arrays and matrices!
    By chwex in forum New To Java
    Replies: 2
    Last Post: 02-15-2013, 01:34 AM
  2. Help with arrays and matrices!
    By chwex in forum New To Java
    Replies: 3
    Last Post: 02-13-2013, 12:46 PM
  3. Arrays and Matrices
    By Shaeman111 in forum New To Java
    Replies: 2
    Last Post: 11-10-2012, 08:24 AM
  4. how to add columns from Matrices(2D array)
    By John_shok in forum New To Java
    Replies: 7
    Last Post: 10-04-2010, 05:33 PM
  5. How to multiply two matrices
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •