Program for addition of two matrices
Last Updated :
16 Feb, 2023
Given two N x M matrices. Find a N x M matrix as the sum of given matrices each value at the sum of values of corresponding elements of the given two matrices.
Approach: Below is the idea to solve the problem.
Iterate over every cell of matrix (i, j), add the corresponding values of the two matrices and store in a single matrix i.e. the resultant matrix.
Follow the below steps to Implement the idea:
- Initialize a resultant matrix res[N][M].
- Run a for loop for counter i as each row and in each iteration:
- Run a for loop for counter j as each column and in each iteration:
- Add values of the two matrices for index i, j and store in res[i][j].
- Return res.
Below is the Implementation of above approach.
C++
#include <bits/stdc++.h>
using namespace std;
#define N 4
void add( int A[][N], int B[][N], int C[][N])
{
int i, j;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
C[i][j] = A[i][j] + B[i][j];
}
int main()
{
int A[N][N] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
int B[N][N] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
int C[N][N];
int i, j;
add(A, B, C);
cout << "Result matrix is " << endl;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++)
cout << C[i][j] << " " ;
cout << endl;
}
return 0;
}
|
C
#include <stdio.h>
#define N 4
void add( int A[][N], int B[][N], int C[][N])
{
int i, j;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
C[i][j] = A[i][j] + B[i][j];
}
int main()
{
int A[N][N] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
int B[N][N] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
int C[N][N];
int i, j;
add(A, B, C);
printf ( "Result matrix is \n" );
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++)
printf ( "%d " , C[i][j]);
printf ( "\n" );
}
return 0;
}
|
Java
class GFG {
static final int N = 4 ;
static void add( int A[][], int B[][], int C[][])
{
int i, j;
for (i = 0 ; i < N; i++)
for (j = 0 ; j < N; j++)
C[i][j] = A[i][j] + B[i][j];
}
public static void main(String[] args)
{
int A[][] = { { 1 , 1 , 1 , 1 },
{ 2 , 2 , 2 , 2 },
{ 3 , 3 , 3 , 3 },
{ 4 , 4 , 4 , 4 } };
int B[][] = { { 1 , 1 , 1 , 1 },
{ 2 , 2 , 2 , 2 },
{ 3 , 3 , 3 , 3 },
{ 4 , 4 , 4 , 4 } };
int C[][] = new int [N][N];
int i, j;
add(A, B, C);
System.out.print( "Result matrix is \n" );
for (i = 0 ; i < N; i++) {
for (j = 0 ; j < N; j++)
System.out.print(C[i][j] + " " );
System.out.print( "\n" );
}
}
}
|
Python3
N = 4
def add(A, B, C):
for i in range (N):
for j in range (N):
C[i][j] = A[i][j] + B[i][j]
A = [[ 1 , 1 , 1 , 1 ],
[ 2 , 2 , 2 , 2 ],
[ 3 , 3 , 3 , 3 ],
[ 4 , 4 , 4 , 4 ]]
B = [[ 1 , 1 , 1 , 1 ],
[ 2 , 2 , 2 , 2 ],
[ 3 , 3 , 3 , 3 ],
[ 4 , 4 , 4 , 4 ]]
C = A[:][:]
add(A, B, C)
print ( "Result matrix is" )
for i in range (N):
for j in range (N):
print (C[i][j], " " , end = '')
print ()
|
C#
using System;
class GFG {
static int N = 4;
static void add( int [, ] A, int [, ] B, int [, ] C)
{
int i, j;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
C[i, j] = A[i, j] + B[i, j];
}
public static void Main()
{
int [, ] A = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
int [, ] B = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
int [, ] C = new int [N, N];
int i, j;
add(A, B, C);
Console.WriteLine( "Result matrix is " );
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++)
Console.Write(C[i, j] + " " );
Console.WriteLine();
}
}
}
|
PHP
<?php
function add(& $A , & $B , & $C )
{
$N = 4;
for ( $i = 0; $i < $N ; $i ++)
for ( $j = 0; $j < $N ; $j ++)
$C [ $i ][ $j ] = $A [ $i ][ $j ] +
$B [ $i ][ $j ];
}
$A = array ( array (1, 1, 1, 1),
array (2, 2, 2, 2),
array (3, 3, 3, 3),
array (4, 4, 4, 4));
$B = array ( array (1, 1, 1, 1),
array (2, 2, 2, 2),
array (3, 3, 3, 3),
array (4, 4, 4, 4));
$N = 4;
add( $A , $B , $C );
echo "Result matrix is \n" ;
for ( $i = 0; $i < $N ; $i ++)
{
for ( $j = 0; $j < $N ; $j ++)
{
echo $C [ $i ][ $j ];
echo " " ;
}
echo "\n" ;
}
?>
|
Javascript
<script>
let N = 4;
function add(A, B, C)
{
let i, j;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
C[i][j] = A[i][j] + B[i][j];
}
let A = [ [1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4]];
let B = [ [1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4]];
let C = new Array(N);
for (let k = 0; k < N; k++)
C[k] = new Array(N);
let i, j;
add(A, B, C);
document.write( "Result matrix is <br>" );
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
document.write(C[i][j] + " " );
document.write( "<br>" );
}
</script>
|
Output
Result matrix is
2 2 2 2
4 4 4 4
6 6 6 6
8 8 8 8
Time complexity: O(n2).
Auxiliary space: O(n2). since n2 extra space has been taken for storing results
The program can be extended for rectangular matrices. The following post can be useful for extending this program.
How to pass a 2D array as a parameter in C?
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...