Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

adding two matrix

#include<stdio.h>
void main(){
    int r,c,i,j,a[100][100],b[100][100],sum[100][100];
    printf("Enter number of rows : ");
    scanf("%d",&r);
    printf("Enter number of columns : ");
    scanf("%d",&c);
    
    printf("##### First Matrix ###### 
");
    for(i=0;i<r;i++){
        for(j=0;j<c;j++){
            printf("Enter element a %d%d : ",i+1,j+1);
            scanf("%d",&a[i][j]);
        }
    }
    printf("##### Second Matrix ###### 
");
    for(i=0;i<r;i++){
        for(j=0;j<c;j++){
            printf("Enter element a %d%d : ",i+1,j+1);
            scanf("%d",&b[i][j]);
        }
    }
    //additon of two matrix
    for(i=0;i<r;i++){
        for(j=0;j<c;j++){
            sum[i][j] = a[i][j] + b[i][j];
        }
    }
    //to show the resultant matrix
    for(i=0;i<r;i++){
        for(j=0;j<c;j++){
            printf("%d	",sum[i][j]);
        
        if(j == c-1){
            printf("
");
        }
        }
    }
    
}
Comment

Addition of matrix

#include<stdio.h>
int main()
{
  int n,m,i,j,a[100][100],b[100][100];
  printf("Enter row and col:");
  scanf("%d%d",&n,&m);
  printf("Matrix A :
");
  for(i=0;i<n;i++)
    {
      for(j=0;j<m;j++)
        {
          printf("a[%d][%d] : ",i,j);
          scanf("%d",&a[i][j]);
        }
    }

   printf("Matrix B :
");
  for(i=0;i<n;i++)
    {
      for(j=0;j<m;j++)
        {
          printf("b[%d][%d] : ",i,j);
          scanf("%d",&b[i][j]);
        }
    }
  printf("Sum of matrix :
");
  for(i=0;i<n;i++)
    {
      for(j=0;j<m;j++)
        {
          printf("%d	",(a[i][j]+b[i][j]));
        }
      printf("
");
    }
}
Comment

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.)
");
        raws = scanner.nextInt();
        System.out.println("(Enter number of columns).
");
        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("]");
        }
    }



}
Comment

PREVIOUS NEXT
Code Example
Java :: metodi di javascritp 
Java :: api to accept a csv file spring boot 
Java :: list all managed beans in spring 
Java :: Artemis agent/client auto failover 
Java :: satck size in java 
Java :: accessdeniedexception amplify init 
Java :: Java Maven Using platform encoding (UTF-8 actually) to read mojo metadata, i.e. build is platform dependent! 
Java :: bucle for java 
Java :: efficient generic duplicate finding class java 
Java :: connect 2 package in android 
Java :: Returning methods 
Java :: Kotlin Toast : error-none-of-the-following-functions-can-be-called-with-the-arguments-supplied 
Java :: url encode not working spring boot oauth2 
Java :: sort stream by key java 
Java :: Java socket connect to gmail 
Java :: hint property of plain text in android studio 
Java :: spring boot endpoint getting list from the body 
Java :: javax.annotation.PreDestroy cannot be found by org.eclipse.e4. 
Java :: least count of words required to construct a target string 
Java :: java access enum per index 
Java :: android studio doesnt work when in full screen mac os 
Java :: android volley benefits 
Java :: how to search element in jtablejava 
Java :: java get current method name 
Java :: Java private no-arg constructor 
Java :: check if object is a string java 
Java :: Retrieve Image from java database. 
Java :: setBackgrounfTint color to relative layout from java file 
Java :: minecraft 21w14a bugs 
Java :: añadir objetos de diferentes clases en una misma lista 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =