Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Two 2d Arrays Sum

I’m trying to take two 2d array inputs, and then add them together, but it won’t compile correctly. For some reason if i declare the ‘addedMatrix’ outside of the method the code compiles but the addedMatrix is blank

Heres what I have so far :

System.out.print("Enter Matrix 1 : ");
double[][] matrix1 = new double[3][3];

for (int r = 0; r < matrix1.length; r++)
{
    for (int c = 0; c < matrix1[0].length; c++)
    {
        matrix1[r][c] = input.nextDouble();
    }
}

System.out.print("Enter Matrix 2 : ");
double[][] matrix2 = new double[3][3];

for (int r = 0; r < matrix2.length; r++)
{
    for (int c = 0; c < matrix2[0].length; c++)
    {
        matrix2[r][c] = input.nextDouble();
    }
}

addMatrix(matrix1, matrix2);

System.out.println("The matrices added are : ");

for (int r = 0; r < addedMatrix.length; r++)
{
    for (int c = 0; c < addedMatrix[0].length; c++)
    {
        System.out.print(addedMatrix[r][c]+ " ");
    }

    System.out.println();
}

public static double[][] addMatrix(double[][] arr1, double[][] arr2)
{
    double[][] addedMatrix = new double[3][3];
        
    for (int r = 0; r < addedMatrix.length; r++)
    {
        for (int c = 0; c < addedMatrix[0].length; c++)
        {
            addedMatrix[r][c] = arr1[r][c] + arr2[r][c];
        }
    }
        
    return addedMatrix;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your syntax is wrong completely firstly you need a main function and then you also need to accept the return type.

import java.util.Scanner;

public class Test {


public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter Matrix 1 : ");
    double[][] matrix1 = new double[3][3];
    for(int r = 0; r < matrix1.length; r++)
    {
        for(int c = 0; c < matrix1[0].length; c++)
        {
            matrix1[r][c] = input.nextDouble();
        }
    }
    System.out.print("Enter Matrix 2 : ");
    double[][] matrix2 = new double[3][3];
    for(int r = 0; r < matrix2.length; r++)
    {
        for(int c = 0; c < matrix2[0].length; c++)
        {
            matrix2[r][c] = input.nextDouble();
        }
    }

    double[][] addedMatrix = addMatrix(matrix1, matrix2);

    System.out.println("The matrices added are : ");
    for(int r = 0; r < addedMatrix.length; r++)
    {
        for(int c = 0; c < addedMatrix[0].length; c++)
        {
            System.out.print(addedMatrix[r][c]+ " ");
        }
        System.out.println();
    }
}

public static double[][] addMatrix(double[][] arr1, double[][] arr2)
{
    double[][] addedMatrix = new double[3][3];
    for(int r = 0; r < addedMatrix.length; r++)
    {
        for(int c = 0; c < addedMatrix[0].length; c++)
        {
            addedMatrix[r][c] = arr1[r][c] + arr2[r][c];
        }
    }
    return addedMatrix;
}
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading