🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Matrix addition is one of the fundamental operations in matrix arithmetic. It involves adding corresponding elements of two matrices to produce a third matrix. In this blog post, we'll learn how to write a C++ program that performs the addition of two matrices.
2. Program Overview
1. Define two matrices of the same dimensions.
2. Initialize a result matrix with the same dimensions.
3. Traverse each row and column.
4. For each element, add corresponding elements of the two matrices and store the result in the result matrix.
5. Display the result matrix.
3. Code Program
#include <iostream>
using namespace std;
int main() {
int r, c, a[100][100], b[100][100], sum[100][100];
cout << "Enter the number of rows and columns of matrices: ";
cin >> r >> c;
cout << "\nEnter elements of the 1st matrix:\n";
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j)
cin >> a[i][j];
cout << "\nEnter elements of the 2nd matrix:\n";
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j)
cin >> b[i][j];
// Adding the two matrices
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];
cout << "\nSum of the matrices:\n";
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
cout << sum[i][j] << " ";
if (j == c - 1)
cout << endl;
}
}
return 0;
}
Output:
Enter the number of rows and columns of matrices: 2 2 Enter elements of the 1st matrix: 1 2 3 4 Enter elements of the 2nd matrix: 5 6 7 8 Sum of the matrices: 6 8 10 12
4. Step By Step Explanation
1. Matrix Initialization: We start by defining and taking input for two matrices a and b of dimensions r x c.
2. Addition Loop: We traverse each element of both matrices and add their corresponding elements, storing the result in the sum matrix.
3. Output: We print out the resulting sum matrix.
Matrix addition is an elemental operation and forms the basis for many complex matrix manipulations in linear algebra.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment