Answers for "adding two matrix"

1

add two matrix

#include<iostream>
using namespace std;

int main() {
    int Matrix_A[3][3] = { { 2,3,4 },
                           { 5, 3, 4 },
                            { 4, 8, 9 }   };

    int Matrix_B[3][3] = { {3,4,5},
                         {7,8,9},
                          {2,3,4} };
    int Matrix_C[3][3];
    // for addition
 

            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                Matrix_C[i][j] =  Matrix_A[i][j] + Matrix_B[i][j];
                }
        }
           
            cout << " THis is the Adding of two Matrix " << endl;

            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    cout << Matrix_A[i][j] << " + " << Matrix_B[i][j] << " = " << Matrix_C[i][j] << endl;
                }
            }

// 


}
Posted by: Guest on April-21-2021
0

adding matrix

import java.util.Scanner;

public class MatrixAddition {
    private static Scanner scanner = new Scanner(System.in);
    private double [][] arr;
    private int columns ;
    private int raws ;
    private static int numberOfMat = 1;


    public void  setRawAndColumns(int raws, int columns){
        System.out.println("(Enter number of raws.)\n");
        raws = scanner.nextInt();
        System.out.println("(Enter number of columns).\n");
        columns = scanner.nextInt();
        SetMatrix(raws,columns);
    }

    private void SetMatrix(int r , int c){
        this.raws = r;
        this.columns = c;
        this.arr = new double[raws][columns];
        enterValues(this.raws,this.columns,this.arr);
        double [][] nextMat = new double[this.raws][this.columns];
        enterValues(this.raws,this.columns, nextMat);
        calculateMatrix(this.arr,nextMat);
    }

    private double[][] enterValues(int r , int c, double[][]arr){
        System.out.println("Enter Values for matrix #"+(this.numberOfMat));
        ++this.numberOfMat;
        for(int i = 0 ; i < arr.length ; ++i ){
            for(int j = 0; j < arr[0].length ; ++j ){
                System.out.println("raw #"+(i+1)+" Column #"+(j+1));
                arr[i][j]= scanner.nextDouble();
            }
        }
        return arr;
    }
    private double[][] calculateMatrix(double[][] mat,double nextMat[][]){
        double [] [] sum = new double[this.raws][this.columns];
        for(int i = 0 ; i < mat.length ; ++i){
            for(int j = 0 ; j < mat[0].length ; ++j){
                sum[i][j]+= (mat[i][j]+nextMat[i][j]);
            }
        }
        printmatrix(sum);
        return null;
    }

    private void printmatrix(double [][] sum){
        for(int i = 0 ; i < sum.length; ++i){
            System.out.print("[ ");
            for (int j = 0 ; j < sum[0].length ; ++j){
                System.out.print(sum[i][j]+" ");
            }
            System.out.println("]");
        }
    }



}
Posted by: Guest on July-27-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language