Use of Listing
Use of Listing
Bipul Mainali
June 10, 2023
List of Code
1 Program to add two Matrix in C Programming . . . . . . . . . . 1
2 Sample in Program C Program . . . . . . . . . . . . . . . . . . . 3
3 Sample in C++ Program . . . . . . . . . . . . . . . . . . . . . . 5
4 Sample in Python Program . . . . . . . . . . . . . . . . . . . . . 7
5 Sample in Matlab Program . . . . . . . . . . . . . . . . . . . . . 8
1
27 int main () {
int mat1 [ MAX_SIZE ][ MAX_SIZE ] , mat2 [ MAX_SIZE ][ MAX_SIZE
] , result [ MAX_SIZE ][ MAX_SIZE ];
29 int rows , columns ;
31 printf ( " Enter the number of rows and columns for the
matrices ( max size : % d ) : " , MAX_SIZE ) ;
scanf ( " % d % d " , & rows , & columns ) ;
33
printf ( " Enter the elements of the first matrix :\ n " ) ;
35 for ( int i = 0; i < rows ; i ++) {
for ( int j = 0; j < columns ; j ++) {
37 scanf ( " % d " , & mat1 [ i ][ j ]) ;
}
39 }
2
Code 2: Sample in Program C Program
1 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Program to add two Matrix in C Programming
3 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# include < stdio .h >
5
# define MAX_SIZE 10
7
// Function to add two matrices
9 void addMatrices ( int mat1 [][ MAX_SIZE ] , int mat2 [][ MAX_SIZE ] ,
int result [][ MAX_SIZE ] , int rows , int columns ) {
for ( int i = 0; i < rows ; i ++) {
11 for ( int j = 0; j < columns ; j ++) {
result [ i ][ j ] = mat1 [ i ][ j ] + mat2 [ i ][ j ];
13 }
}
15 }
27 int main () {
int mat1 [ MAX_SIZE ][ MAX_SIZE ] , mat2 [ MAX_SIZE ][ MAX_SIZE ] ,
result [ MAX_SIZE ][ MAX_SIZE ];
29 int rows , columns ;
31 printf ( " Enter the number of rows and columns for the
matrices ( max size : % d ) : " , MAX_SIZE ) ;
scanf ( " % d % d " , & rows , & columns ) ;
33
printf ( " Enter the elements of the first matrix :\ n " ) ;
35 for ( int i = 0; i < rows ; i ++) {
for ( int j = 0; j < columns ; j ++) {
37 scanf ( " % d " , & mat1 [ i ][ j ]) ;
}
39 }
3
45 }
}
47
// Add the matrices
49 addMatrices ( mat1 , mat2 , result , rows , columns ) ;
4
Code 3: Sample in C++ Program
1 # include < iostream >
using namespace std ;
3
const int MAX_SIZE = 10;
5
// Function to add two matrices
7 void addMatrices ( int mat1 [][ MAX_SIZE ] , int mat2 [][ MAX_SIZE ] ,
int result [][ MAX_SIZE ] , int rows , int columns ) {
for ( int i = 0; i < rows ; i ++) {
9 for ( int j = 0; j < columns ; j ++) {
result [ i ][ j ] = mat1 [ i ][ j ] + mat2 [ i ][ j ];
11 }
}
13 }
25 int main () {
int mat1 [ MAX_SIZE ][ MAX_SIZE ] , mat2 [ MAX_SIZE ][ MAX_SIZE ] ,
result [ MAX_SIZE ][ MAX_SIZE ];
27 int rows , columns ;
29 cout << " Enter the number of rows and columns for the
matrices ( max size : " << MAX_SIZE << " ) : " ;
cin >> rows >> columns ;
31
cout << " Enter the elements of the first matrix : " <<
endl ;
33 for ( int i = 0; i < rows ; i ++) {
for ( int j = 0; j < columns ; j ++) {
35 cin >> mat1 [ i ][ j ];
}
37 }
39 cout << " Enter the elements of the second matrix : " <<
endl ;
for ( int i = 0; i < rows ; i ++) {
41 for ( int j = 0; j < columns ; j ++) {
cin >> mat2 [ i ][ j ];
5
43 }
}
45
// Add the matrices
47 addMatrices ( mat1 , mat2 , result , rows , columns ) ;
49 cout << " The sum of the matrices is : " << endl ;
printMatrix ( result , rows , columns ) ;
51
return 0;
53 }
6
Code 4: Sample in Python Program
1 def add_matrices ( mat1 , mat2 ) :
rows = len ( mat1 )
3 columns = len ( mat1 [0])
11 return result
13
def print_matrix ( matrix ) :
15 for row in matrix :
for element in row :
17 print ( element , end = " " )
print ()
19
7
Code 5: Sample in Matlab Program
function matrix = input_matrix ( rows , columns )
2 matrix = zeros ( rows , columns ) ;
for i = 1: rows
4 for j = 1: columns
fprintf ( ’ Enter element (% d , % d ) : ’ , i , j ) ;
6 matrix (i , j ) = input ( ’ ’) ;
end
8 end
end