🎓 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 multiplication is a binary operation that produces a matrix from two matrices. For matrix multiplication to be defined, the number of columns in the first matrix should be equal to the number of rows in the second matrix. In this guide, we'll walk you through a C program that multiplies two matrices, followed by a step-by-step explanation of the code.
2. Program Overview
The C program will:
1. Ask the user for the dimensions of the two matrices.
2. Ensure matrix multiplication is possible based on the given dimensions.
3. Take the elements of both matrices as input from the user.
4. Multiply the matrices.
5. Display the resulting matrix.
3. Code Program
#include <stdio.h>
int main() {
int a[10][10], b[10][10], result[10][10];
int aRows, aCols, bRows, bCols;
int i, j, k, sum;
// Asking user to input the dimensions of the matrices
printf("Enter number of rows and columns for first matrix: ");
scanf("%d%d", &aRows, &aCols);
printf("Enter number of rows and columns for second matrix: ");
scanf("%d%d", &bRows, &bCols);
if (aCols != bRows) {
printf("Matrix multiplication not possible!");
return 0;
}
// Reading elements of first matrix
printf("Enter elements of first matrix:\n");
for(i = 0; i < aRows; i++) {
for(j = 0; j < aCols; j++) {
scanf("%d", &a[i][j]);
}
}
// Reading elements of second matrix
printf("Enter elements of second matrix:\n");
for(i = 0; i < bRows; i++) {
for(j = 0; j < bCols; j++) {
scanf("%d", &b[i][j]);
}
}
// Multiplying the matrices
for(i = 0; i < aRows; i++) {
for(j = 0; j < bCols; j++) {
sum = 0;
for(k = 0; k < aCols; k++) {
sum += a[i][k] * b[k][j];
}
result[i][j] = sum;
}
}
// Displaying the result
printf("Resultant matrix:\n");
for(i = 0; i < aRows; i++) {
for(j = 0; j < bCols; j++) {
printf("%d\t", result[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Enter number of rows and columns for first matrix: 2 2 Enter number of rows and columns for second matrix: 2 2 Enter elements of first matrix: 1 2 3 4 Enter elements of second matrix: 5 6 7 8 Resultant matrix: 19 22 43 50
4. Step By Step Explanation
1. Variable Declaration:
- Three matrices a, b, and result are declared.
- aRows, aCols, bRows, and bCols store the dimensions of matrices a and b, respectively.
2. Getting Dimensions and Input:
- The program prompts the user to provide the dimensions for both matrices.
- A check is performed to ensure the number of columns in the first matrix equals the number of rows in the second matrix.
- The user is then asked to input the elements of both matrices.
3. Matrix Multiplication:
- Three nested loops are used to perform the matrix multiplication.
- The outer two loops iterate through the rows of the first matrix and the columns of the second matrix.
- The innermost loop, indexed by k, performs the actual multiplication and accumulation for each element of the result matrix.
4. Displaying the Result:
- The program then outputs the multiplied matrix.
Matrix multiplication is a fundamental operation in linear algebra and finds numerous applications in various mathematical, scientific, and engineering domains.
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