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 11 of 11

Thread: Help with Multi-dimensional Arrays. Help needed urgently!

  1. #1
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Question Help with Multi-dimensional Arrays. Help needed urgently!

    I am having trouble fixing and figuring out how to change my code. My out put is very off, and I was wondering if anyone could give me insight to why, maybe even pose a solution,

    "Enter a 3-by-3 matrix row by row:
    1 1 1
    1 1 1
    1 1 1
    Sum of the major diagonal is 6.0
    Sum of the values of the column are: 10.0
    Sum of the values of the column are: 20.0
    Sum of the values of the column are: 30.0 "

    I have tried a lot of things, but not much has helped.
    Thank you and any help is greatly appreciated.

    Code is below:
     
        public static void main(String[] args) {
            double[][] m = new double[3][3];
            m = createArray();  
     
        }    
        private static double[][] createArray() { 
     
        Scanner input = new Scanner(System.in);
        double[][] matrix = new double[3][3];
        System.out.println("Enter a " + matrix.length + "-by-" + matrix[0].length +
                    " matrix row by row: ");
        for (double[] m1 : matrix) {
            for (int j = 0; j < matrix[0].length; j++) {
     
                m1[j] = input.nextDouble();
     
            }
        } 
        System.out.println("Sum of the major diagonal is " + sumMajorDiagonal(matrix));
        sumColumn (matrix, 0); 
            return matrix;
        }  
     
     public static double sumColumn(double[][] matrix, int columnIndex){
     
       double total = 0;//problem lies here
       double sum = 0;
       int row = 0;
       int last = matrix[0].length;
       for(columnIndex = 0; columnIndex < matrix[row].length; columnIndex++) {
            //total += matrix[0][1] + [1][1];
            total += matrix[row][--last];
       System.out.println("Sum of the values of the column are: " + (columnIndex + 1)
            + sum);   
     }
     
          return sum;  
    } 
     
    public static double sumMajorDiagonal(double[][] m) {
     
        double total = 0.0;
        int last = m[0].length;
     
        for(int row = 0; row < m.length; row++) {
            total += m[row][row];
            total += m[row][--last];
    }
      return total;    
     } 
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Multi-dimensional Arrays. Help needed urgently!

    My out put is very off,
    Can you explain what is wrong with the output and give an example of what it should be?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Knowledge_Feeds_The_Mind (April 22nd, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with Multi-dimensional Arrays. Help needed urgently!

    You're not concerned about the sum of the major diagonal?

    I don't understand the purpose for the variable 'last'. Can you describe what that does? Oh yeah, that reminds me: comments in your code would be nice, probably even helpful to you and certainly helpful to those reviewing your code.

    You have too many things going on in some of your methods. Methods other than main() should do one thing. The one thing they do should be described by the name of the method, usually a verb or containing a verb. The one thing each method does should be done very well.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    Knowledge_Feeds_The_Mind (April 22nd, 2014)

  6. #4
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Help with Multi-dimensional Arrays. Help needed urgently!

    Thank you very much for the fast response.
    my output needs to look some thing like this. like this:

    "Enter a 3-by-3 matrix row by row:
    Row 1: 1 2 3
    Row 2: 5 6 7
    Row 3: 9 1 3

    The sum of the elements at Column 1 is 16
    The sum of the elements at Column 2 is 9
    The sum of the elements at Column 3 is 13

    The sum of the elements along the major diagonal is 10"

    My main plan of action is to first get the problem figured out with the sum of the columns and then later, tackle the major diagonal.
    I am currently trying to figure out what is exactly wrong with my methods.....and what is causing this incorrect results to appear.

     
    //Write a program that reads a 3 by 3 array of double values row by row, stores
    //values into a two dimensional array of three rows by three columns
     
      public static void main(String[] args) {
            double[][] matrix = new double[3][3];// the 3 - 3 matrix
            matrix = createArray();  
     
        }    
        private static double[][] createArray() { 
     
        Scanner input = new Scanner(System.in);
        double[][] matrix = new double[3][3];
        System.out.println("Enter a 3-by-3 matrix row by row ");
        for (double[] m1 : matrix) {
        System.out.println("Row: ");
            for (int j = 0; j < matrix[0].length; j++) {
     
                m1[j] = input.nextDouble();
     
            }
        } 
        System.out.println("Sum of the major diagonal: " + sumMajorDiagonal(matrix));
        sumColumn (matrix, 0); 
            return matrix;
        }  
     
     public static double sumColumn(double[][] matrix, int columnIndex){
     
       double sum = 0;
       for (int column = 0; column < matrix.length; column++) {
          for (int row = 0; row < matrix[column].length; row++){
             sum += matrix[row][column];  
    }
       System.out.println("Sum of 1st column is " + column + sum); 
    } 
            return sum;
    }
     
    public static double sumMajorDiagonal(double[][] matrix) {
     
        double sum = 0.0;
        int last = matrix[0].length;
     
        for(int row = 0; row < matrix.length; row++) {
            sum += matrix[row][row];
            sum += matrix[row][--last];  //calculate the last value and ends 
    }
    return sum; 
    }
    }

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Multi-dimensional Arrays. Help needed urgently!

    The posted code won't compile for testing. It needs the class definition.

    One observation:
           double[][] matrix = new double[3][3];// the 3 - 3 matrix  <<< Here creates an array
            matrix = createArray();  //<<< Here IMMEDIATELY destroys/replaces the array created above
    There is no need to create an array if the next statement is going to replace it. Define it and assign it a value in one step:
    double[][] matrix = createArray();

    The posted code does not contain this String: "The sum of the elements at Column"
    where is the code to do that summation?

    The calls to the methods to work with the array should NOT be nested inside of the create method.
    Each method should be called separately in the main method:
    create the array
    sum this way
    sum that way
    sum the other way

    The created array should be passed as an arg to each of the sum... methods.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Knowledge_Feeds_The_Mind (April 22nd, 2014)

  9. #6
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Help with Multi-dimensional Arrays. Help needed urgently!

    this is the code where the summation is completed:
    Thank you for your assistance

     public static double sumColumn(double[][] matrix, int columnIndex){
     
       double sum = 0;
       //below is the code to solve for the column sum/ summation
       for (int column = 0; column < matrix.length; column++)
       {
          for (int row = 0; row < matrix[column].length; row++)
          {
             sum += matrix[row][column];  
    }
       System.out.println("Sum of 1st column is " + column + sum); 
    } 
            return sum;
    }

  10. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Multi-dimensional Arrays. Help needed urgently!

    What does that code do with the columnIndex value that is passed to it?
    If you were to write a statement to add the contents of column 1 for three rows (0,1,2) of the matrix, what would that statement look like?

    The method should return the value, not print it. The caller of the method should do the printing.
    val = sum() // get the value
    print val // print the value
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Knowledge_Feeds_The_Mind (April 22nd, 2014)

  12. #8
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Help with Multi-dimensional Arrays. Help needed urgently!

    So, I solved it. Now I only need help with calculating the major diagonal

     public static double sumColumn(double[][] matrix, int column){
     
       int row;
       double sum = 0;
       //below is the code to solve for the column 
       for (column = 0; column < matrix[0].length; column++)
       {
        sum = 0;
       for (row = 0; row < matrix.length; row++)
        sum = sum + matrix[row][column];      
       System.out.println("Sum of column " + (column + 1) + " = " + sum);    
    } 
            return sum;
    }

    This is the code that I am still working on. It calculated the diagonal twice instead of only once. Any insights.
    Thank you very much for your patience and assistance, I am deeply appreciative.

    the full code 
     
    public static double sumMajorDiagonal(double[][] matrix) {
     
        double sum = 0.0;
        int last = matrix[0].length;
     
        for(int row = 0; row < matrix.length; row++) {
            sum += matrix[row][row];
            sum += matrix[row][--last];  //calculate the last value and ends 
    }
    return sum; 
    }
    }

  13. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Multi-dimensional Arrays. Help needed urgently!

    sumColumn() should need only one loop to go down the rows and add the values in the selected column.

    In the sumMajorDiagonal() method, What are the elements that should be summed to get the value you want?
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    Knowledge_Feeds_The_Mind (April 22nd, 2014)

  15. #10
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Help with Multi-dimensional Arrays. Help needed urgently!

    Thanks, I finally solved my problem. I really appreciate all the help you have given me!
    If it had not been for your insight it probably would have taken me much longer, thank you so much again.
    -Vex

  16. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Multi-dimensional Arrays. Help needed urgently!

    Glad its working for you now.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Problem with Multi-Dimensional Arrays
    By NickieBoren in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 30th, 2013, 06:32 PM
  2. Grails Developer Needed - URGENTLY NEEDED *WORK FROM HOME*
    By IngeniumR in forum Paid Java Projects
    Replies: 0
    Last Post: February 5th, 2013, 07:19 AM
  3. How to begin with multi-dimensional arrays
    By Lorelai in forum Java Theory & Questions
    Replies: 1
    Last Post: April 6th, 2012, 06:56 PM
  4. Help needed urgently!!!
    By sonia11 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 19th, 2010, 04:19 PM
  5. Multi Dimensional Array help NEEDED URGENT
    By bonjovi4u in forum Loops & Control Statements
    Replies: 5
    Last Post: February 13th, 2010, 12:44 AM