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: multiplication of 2D array , result after multiplication is wrong . what's the error?

  1. #1
    Junior Member
    Join Date
    Jul 2021
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy multiplication of 2D array , result after multiplication is wrong . what's the error?

    package arrays;
    import java.util.Scanner;

    public class UserInputMulti2 {

    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    int rows1, cols1, rows2, cols2;

    int arrayOne [] [] = new int [10] [10];
    int arrayTwo [] [] = new int [10] [10];
    int result [] [] = new int [10] [10];

    System.out.println(" Enter the dimensions of array one : ");
    rows1 = sc.nextInt();
    cols1 = sc.nextInt();

    System.out.println(" Enter the dimensions of array two : ");
    rows2 = sc.nextInt();
    cols2 = sc.nextInt();

    if(cols1 == rows2) {
    System.out.println(" you can multiply ");
    }else {
    System.out.println(" you can't multiply ");
    }
    // taking input for array one
    System.out.println(" Enter the elements of array one : ");

    for(int i = 0 ; i<rows1; i++) {
    for(int j = 0 ; j<cols1; j++) {
    arrayOne[i] [j] = sc.nextInt();
    }
    }
    // taking input for array two
    System.out.println(" Enter the elements of array two : ");

    for(int i = 0 ; i<rows2; i++) {
    for(int j = 0 ; j<cols2; j++) {
    arrayTwo[i] [j] = sc.nextInt();
    }
    }
    // now the process of multiplication .

    for(int i = 0 ; i<rows1; i++) {
    for(int j = 0 ; j<cols1; j++) {
    for(int k = 0 ; k<cols2; k++) {
    result [i] [j] += arrayOne[i] [k] * arrayTwo[k] [j];
    }
    }
    }

    System.out.println(" Result of matrices is : ");
    for(int i = 0 ; i<rows1; i++) {
    for(int j = 0 ; j<cols2; j++) {
    System.out.print(result [i] [j] + " ");
    }
    System.out.println();
    }
    }

    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: multiplication of 2D array , result after multiplication is wrong . what's the error?

    result after multiplication is wrong
    Please explain. Copy the output and paste it here so we can see it. Add some comments that show what is wrong and post the desired/expected output.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2021
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: multiplication of 2D array , result after multiplication is wrong . what's the error?

    in this i will take two 2Darray from user than multiply both of them and shows the output on screen.
    the error is that , it prints 0 0 0 in last column of resultant array which comes after multiplication. like.

    23 13 0
    65 25 0
    15 76 0

    like this all the values of column 3 are comes 0 .

    check , if you find any mistake please comment down below.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: multiplication of 2D array , result after multiplication is wrong . what's the error?

    the error is that , it prints 0 0 0 in last column
    Can you show the input that generated those values in post#3?
    What were the expected values?

    How are you trying to debug the code to find out where the problem is?
    I use print statements to show the values of any variables that are used to see if they are what is expected.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. multiplication table java 2D array
    By WebNewBie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 14th, 2019, 09:58 PM
  2. What's wrong with the code? Multiplication of Matrices
    By ksheth94 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2013, 12:33 PM
  3. Multiplication program Loop
    By PhilThomspon in forum Loops & Control Statements
    Replies: 7
    Last Post: May 6th, 2012, 03:29 PM
  4. Multiplication program
    By PhilThomspon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 6th, 2012, 01:54 PM
  5. Multiplication code
    By bomboy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 5th, 2011, 02:25 AM