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: Multidimensional user defined array question

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Multidimensional user defined array question

    The program below asks the user for the size of the multidimensional array and creates it. Then, I want to add the total of each rows and display it. I'm having trouble with that. My program adds the total from the previous row and add it to the current total. How can I fix this?

    This is what it should look like

    How big would you like your matrix?
    	Rows - ? 2
    	Cols - ? 2
     
    Please enter your row 1?
    	Column 1 - 1
    	Column 2 - 2
     
    Please enter your row 2?
    	Column 1 - 3
    	Column 2 - 4
     
    Your total for row 1 is - 3
    Your total for row 2 is - 7
     
    Your total for the 2 x 2 matrix is 10

    This is what it looks like now


    How big would you like your matrix?
    	Rows - ? 2
    	Cols - ? 2
     
    Please enter your row 1?
    	Column 1 - 1
    	Column 2 - 2
     
    Please enter your row 2?
    	Column 1 - 3
    	Column 2 - 4
     
    Your total for row 1 is - 3
    Your total for row 2 is - 10
     
    Your total for the 2 x 2 matrix is 13

    import java.util.Scanner;
     
    public class ArraySum {
     
        public static void main(String[] args) {
     
            Scanner scan = new Scanner(System.in);
     
            int rows, cols, colValue, rowTotal, matrixTotal;
     
            rowTotal = 0;
            matrixTotal = 0;
     
            System.out.println("How big would you like your matrix?");
            System.out.print("\tRows - ? ");
            rows = scan.nextInt();
            System.out.print("\tCols - ? ");
            cols = scan.nextInt();
     
            System.out.println();
     
            int[][] numberArray = new int[rows][cols];
     
            for(int a=0; a<numberArray.length; a++)
            {
                System.out.println("Please enter your row " + (a+1) + "?");
     
                for(int b=0; b<numberArray[0].length; b++)
                {
                    System.out.print("\tColumn " + (b+1) + " - ");
                    colValue = scan.nextInt();    
     
                    numberArray[a][b] = colValue;
                }
     
                System.out.println();
            }            
     
            for(int c=0; c<numberArray.length; c++)
            {            
                for(int d=0; d<numberArray[0].length; d++)
                {                               
                    rowTotal = rowTotal + numberArray[c][d];    
                }   
     
                matrixTotal = matrixTotal + rowTotal;
     
                System.out.println("Your total for row " + (c+1) + " is - " + rowTotal);
            }   
     
            System.out.println();
     
            System.out.println("Your total for the " + rows + " x " + cols + " matrix is " + matrixTotal);
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,304
    My Mood
    Mellow
    Thanks
    252
    Thanked 778 Times in 763 Posts

    Default Re: Multidimensional user defined array question

    It appears that you need to reset rowTotal to zero between trips through the nested for loop.

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

    tonynsx (November 6th, 2013)

  4. #3
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Multidimensional user defined array question

    Quote Originally Posted by GregBrannon View Post
    It appears that you need to reset rowTotal to zero between trips through the nested for loop.
    Thank you, I didn't think about that, it works now.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,304
    My Mood
    Mellow
    Thanks
    252
    Thanked 778 Times in 763 Posts

    Default Re: Multidimensional user defined array question

    You're welcome. Glad to help.

Similar Threads

  1. LinkedList with user defined types
    By muhammadabrar78 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 22nd, 2013, 10:15 PM
  2. User Defined Criteria
    By SimonTemplar in forum Java Theory & Questions
    Replies: 0
    Last Post: February 20th, 2013, 07:02 PM
  3. User-Defined Methods
    By ZippyShannon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 28th, 2011, 11:23 PM
  4. User Defined Methods
    By mgutierrez19 in forum Object Oriented Programming
    Replies: 11
    Last Post: October 20th, 2009, 07:57 PM