0% found this document useful (0 votes)
20 views8 pages

Use of Listing

The document provides code samples for including source code in LaTeX documents. It includes 5 code samples showing how to add matrices in C, C++, Python, and MATLAB. The C code sample shows how to define functions to add and print matrices, take input for two matrices, add them and print the result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

Use of Listing

The document provides code samples for including source code in LaTeX documents. It includes 5 code samples showing how to add matrices in C, C++, Python, and MATLAB. The C code sample shows how to define functions to add and print matrices, take input for two matrices, add them and print the result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

How to include source code in LATEX

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

Code 1: Program to add two Matrix in C Programming


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 }

17 // Function to print a matrix


void printMatrix ( int mat [][ MAX_SIZE ] , int rows , int
columns ) {
19 for ( int i = 0; i < rows ; i ++) {
for ( int j = 0; j < columns ; j ++) {
21 printf ( " % d " , mat [ i ][ j ]) ;
}
23 printf ( " \ n " ) ;
}
25 }

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 }

41 printf ( " Enter the elements of the second matrix :\ n " ) ;


for ( int i = 0; i < rows ; i ++) {
43 for ( int j = 0; j < columns ; j ++) {
scanf ( " % d " , & mat2 [ i ][ j ]) ;
45 }
}
47
// Add the matrices
49 addMatrices ( mat1 , mat2 , result , rows , columns ) ;

51 printf ( " The sum of the matrices is :\ n " ) ;


printMatrix ( result , rows , columns ) ;
53
return 0;
55 }

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 }

17 // Function to print a matrix


void printMatrix ( int mat [][ MAX_SIZE ] , int rows , int columns )
{
19 for ( int i = 0; i < rows ; i ++) {
for ( int j = 0; j < columns ; j ++) {
21 printf ( " % d " , mat [ i ][ j ]) ;
}
23 printf ( " \ n " ) ;
}
25 }

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 }

41 printf ( " Enter the elements of the second matrix :\ n " ) ;


for ( int i = 0; i < rows ; i ++) {
43 for ( int j = 0; j < columns ; j ++) {
scanf ( " % d " , & mat2 [ i ][ j ]) ;

3
45 }
}
47
// Add the matrices
49 addMatrices ( mat1 , mat2 , result , rows , columns ) ;

51 printf ( " The sum of the matrices is :\ n " ) ;


printMatrix ( result , rows , columns ) ;
53
return 0;
55 }

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 }

15 // Function to print a matrix


void printMatrix ( int mat [][ MAX_SIZE ] , int rows , int columns )
{
17 for ( int i = 0; i < rows ; i ++) {
for ( int j = 0; j < columns ; j ++) {
19 cout << mat [ i ][ j ] << " " ;
}
21 cout << endl ;
}
23 }

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])

5 result = [[0] * columns for _ in range ( rows ) ]

7 for i in range ( rows ) :


for j in range ( columns ) :
9 result [ i ][ j ] = mat1 [ i ][ j ] + mat2 [ i ][ j ]

11 return result

13
def print_matrix ( matrix ) :
15 for row in matrix :
for element in row :
17 print ( element , end = " " )
print ()
19

21 # Get the dimensions of the matrices


rows = int ( input ( " Enter the number of rows : " ) )
23 columns = int ( input ( " Enter the number of columns : " ) )

25 # Get the elements of the first matrix


print ( " Enter the elements of the first matrix : " )
27 matrix1 = []
for _ in range ( rows ) :
29 row = list ( map ( int , input () . split () ) )
matrix1 . append ( row )
31
# Get the elements of the second matrix
33 print ( " Enter the elements of the second matrix : " )
matrix2 = []
35 for _ in range ( rows ) :
row = list ( map ( int , input () . split () ) )
37 matrix2 . append ( row )

39 # Add the matrices


result = add_matrices ( matrix1 , matrix2 )
41
# Print the sum of the matrices
43 print ( " The sum of the matrices is : " )
print_matrix ( result )

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

You might also like