Introduction
In this lab, you will learn how to add two matrices in C programming. The lab covers the following steps: reading the dimensions and elements of both matrices, adding the corresponding elements, and printing the resulting matrix. You will start by creating a C program to read the matrix dimensions and elements, then implement the matrix addition logic, and finally output the final matrix. The lab aims to provide a hands-on experience in working with matrices and linear algebra using C.
Read Dimensions and Elements of Both Matrices
In this step, you will learn how to read the dimensions and elements of two matrices in a C program. Matrix addition requires matrices of the same size, so we'll first implement input for matrix dimensions and elements.
Create the Matrix Addition Program
First, create a new C file for matrix addition:
cd ~/project
nano matrix_addition.c
Now, let's write the code to read matrix dimensions and elements:
#include <stdio.h>
#define MAX_SIZE 10
int main() {
int rows, cols;
int matrix1[MAX_SIZE][MAX_SIZE], matrix2[MAX_SIZE][MAX_SIZE];
// Read dimensions of matrices
printf("Enter number of rows (max %d): ", MAX_SIZE);
scanf("%d", &rows);
printf("Enter number of columns (max %d): ", MAX_SIZE);
scanf("%d", &cols);
// Input elements for first matrix
printf("Enter elements of first matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element [%d][%d]: ", i, j);
scanf("%d", &matrix1[i][j]);
}
}
// Input elements for second matrix
printf("Enter elements of second matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element [%d][%d]: ", i, j);
scanf("%d", &matrix2[i][j]);
}
}
return 0;
}
Compile and Run the Program
Compile the program:
gcc matrix_addition.c -o matrix_addition
Example Output
When you run the program, it will prompt you to enter matrix dimensions and elements:
Enter number of rows (max 10): 2
Enter number of columns (max 10): 3
Enter elements of first matrix:
Enter element [0][0]: 1
Enter element [0][1]: 2
Enter element [0][2]: 3
Enter element [1][0]: 4
Enter element [1][1]: 5
Enter element [1][2]: 6
Enter elements of second matrix:
Enter element [0][0]: 7
Enter element [0][1]: 8
Enter element [0][2]: 9
Enter element [1][0]: 10
Enter element [1][1]: 11
Enter element [1][2]: 12
Code Explanation
#define MAX_SIZE 10sets a maximum limit for matrix dimensionsscanf()is used to read integer inputs for rows, columns, and matrix elements- Nested
forloops are used to input elements for both matrices - The program ensures that matrix dimensions are within the defined maximum size
Add Corresponding Elements
In this step, you will learn how to add corresponding elements of two matrices and store the result in a new matrix.
Update the Matrix Addition Program
Open the previous C file:
cd ~/project
nano matrix_addition.c
Modify the program to add matrix elements:
#include <stdio.h>
#define MAX_SIZE 10
int main() {
int rows, cols;
int matrix1[MAX_SIZE][MAX_SIZE], matrix2[MAX_SIZE][MAX_SIZE], result[MAX_SIZE][MAX_SIZE];
// Previous input code remains the same
// ... (input dimensions and elements of matrix1 and matrix2)
// Add corresponding elements
printf("\nResulting Matrix (Matrix1 + Matrix2):\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
Compile and Run the Program
Compile the updated program:
gcc matrix_addition.c -o matrix_addition
Run the program:
./matrix_addition
Example Output
Enter number of rows (max 10): 2
Enter number of columns (max 10): 3
Enter elements of first matrix:
Enter element [0][0]: 1
Enter element [0][1]: 2
Enter element [0][2]: 3
Enter element [1][0]: 4
Enter element [1][1]: 5
Enter element [1][2]: 6
Enter elements of second matrix:
Enter element [0][0]: 7
Enter element [0][1]: 8
Enter element [0][2]: 9
Enter element [1][0]: 10
Enter element [1][1]: 11
Enter element [1][2]: 12
Resulting Matrix (Matrix1 + Matrix2):
8 10 12
14 16 18
Code Explanation
- A new 2D array
resultis created to store the sum of matrix elements - Nested
forloops iterate through each element of both matrices result[i][j] = matrix1[i][j] + matrix2[i][j]adds corresponding elementsprintf()is used to display the resulting matrix
Print the Resulting Matrix
In this step, you will learn how to format and print the resulting matrix in a clean, readable manner.
Enhance the Matrix Printing Function
Open the previous C file:
cd ~/project
nano matrix_addition.c
Update the program with a dedicated matrix printing function:
#include <stdio.h>
#define MAX_SIZE 10
// Function to print matrix
void printMatrix(int matrix[MAX_SIZE][MAX_SIZE], int rows, int cols, const char* matrixName) {
printf("%s:\n", matrixName);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%4d ", matrix[i][j]);
}
printf("\n");
}
printf("\n");
}
int main() {
int rows, cols;
int matrix1[MAX_SIZE][MAX_SIZE], matrix2[MAX_SIZE][MAX_SIZE], result[MAX_SIZE][MAX_SIZE];
// Previous input code remains the same
// ... (input dimensions and elements of matrix1 and matrix2)
// Add corresponding elements
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Print matrices
printMatrix(matrix1, rows, cols, "First Matrix");
printMatrix(matrix2, rows, cols, "Second Matrix");
printMatrix(result, rows, cols, "Resulting Matrix (Matrix1 + Matrix2)");
return 0;
}
Compile and Run the Program
Compile the updated program:
gcc matrix_addition.c -o matrix_addition
Run the program:
./matrix_addition
Example Output
Enter number of rows (max 10): 2
Enter number of columns (max 10): 3
Enter elements of first matrix:
Enter element [0][0]: 1
Enter element [0][1]: 2
Enter element [0][2]: 3
Enter element [1][0]: 4
Enter element [1][1]: 5
Enter element [1][2]: 6
Enter elements of second matrix:
Enter element [0][0]: 7
Enter element [0][1]: 8
Enter element [0][2]: 9
Enter element [1][0]: 10
Enter element [1][1]: 11
Enter element [1][2]: 12
First Matrix:
1 2 3
4 5 6
Second Matrix:
7 8 9
10 11 12
Resulting Matrix (Matrix1 + Matrix2):
8 10 12
14 16 18
Code Explanation
printMatrix()function is created to print matrices with consistent formatting%4dformat specifier ensures aligned column printing- Function takes matrix, dimensions, and a name as parameters
- Matrices are printed with descriptive headers
- Nested loops iterate through matrix elements for printing
Summary
In this lab, you learned how to read the dimensions and elements of two matrices in a C program. This is an essential step for matrix addition, as it requires matrices of the same size. You implemented input for matrix dimensions and elements, ensuring that the program can handle matrices up to a maximum size of 10x10.
After reading the matrix dimensions and elements, the next step is to add the corresponding elements of the two matrices and store the resulting matrix.



