Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8932979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:25:41+00:00 2026-06-15T09:25:41+00:00

This program adds two 3×3 matrices. It compiles and runs, but the output, instead

  • 0

This program adds two 3×3 matrices. It compiles and runs, but the output, instead of being:

1.0  2.0  3.0       0.0  2.0  4.0       1.0  4.0  7.0
4.0  5.0  6.0   +   1.0  4.5  2.2   =   5.0  9.5  8.2
7.0  8.0  9.0       1.1  4.3  5.2       8.1  12.3 14.2

It produces:

1.0  2.0  3.0       0.0  2.0  4.0       0.0  0.0  0.0
4.0  5.0  6.0   +   1.0  4.5  2.2   =   0.0  0.0  0.0
7.0  8.0  9.0       1.1  4.3  5.2       0.0  0.0  0.0

I’m not sure why the output displays as all zeros? The math in the program ‘seems’ right to me… Is there something I’m missing here?

import java.util.Scanner;

public class AddMatrices{

public static void main(String[] args){

    Scanner input = new Scanner(System.in);

    int N = 3;

    //get the users input and store it in the two arrays
    System.out.println("\nEnter matrix1: \n");

    //declare 2 arrays with the appropriate number of rows and columns in 
    //them to store the numbers in each matrix.
    //this is the first one.
    double[][] matrix1 = new double[N][N];

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

    //get the users input and store it in the two arrays
    System.out.println("\nEnter matrix2: \n");

    //declare 2 arrays with the appropriate number of rows and columns in 
    //them to store the numbers in each matrix.
    //this is the second one.
    double[][] matrix2 = new double[3][3];

    for (int i = 0; i < matrix1.length; i++) {
        for (int j = 0; j < matrix1[i].length; j++) {
            matrix2[i][j] = input.nextDouble();
        }
    }


    //call the addMatrix method and pass it the two arrays
    double[][] resultingMatrix = addMatrix(matrix1, matrix2);
    System.out.println("The addition of the matrices is ");



}//end of main method


//write the addMatrix method to add the two matrices and display the result

public static double[][] addMatrix(double[][] m1, double[][] m2){

    double[][] result = new double[m1.length][m1[0].length];

    for (int i = 0; i < result.length; i++) {
        for (int j = 0; j < result[0].length; j++){
            m1[i][j] += m2[i][j];
        }
    }

    for (int i = 0; i < m1.length; i++) {
        char plus = '+';
        for (int j = 0; j < m1[0].length; j++) {
            System.out.print("  " + m1[i][j]);
        }

        if (i == m1.length / 2)
            System.out.print("  " + plus + "  ");
        else {
            System.out.print("  ");
        }

        for (int j = 0; j < m2[0].length; j++) {
            System.out.print("  " + m2[i][j]);
        }

        if (i == m1.length / 2)
            System.out.print("  =  ");
        else {
            System.out.print("  ");
        }

        for (int j = 0; j < result[0].length; j++) {
            System.out.print("  " + result[i][j]);
        }
        System.out.println();
    }
return result;
}//end of add matrices

}//end of class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:25:43+00:00Added an answer on June 15, 2026 at 9:25 am

    I think you are never assigning the result to the variable results

    you should change

    m1[i][j] += m2[i][j];
    

    to

    result[i][j]=m1[i][j] + m2[i][j];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program adds two matrices but it's giving too many errors and I can't
This program runs just fine, but for some reason when I ask for an
This program builds a dictionary out of a list based on what two index
This program so far has one purpose, take in two integers(the size of the
Now lets see this small program char s[20]=One; strcat(s,Two); cout<<s<<endl; Here at first s
I am making a program that adds two binary numbers (up to 31 digits)
I wrote a simple program that adds two edit fields to the field manager:
This program returns: package main import ( flag fmt ) func main() { num_agents
this program is for class to display the database in Listview.i want to add
This program I use has it's own variables to set when you run it,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.