×

Matrix Multiplication in C

Matrix Multiplication in C

Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix, and second element matrix. Then we do multiplication on the user's entered matrices.

In the first matrix multiplication, the one-row element is multiplied by all column elements in the second matrix.

Matrix Multiplication in C

#include<stdio.h>
 int main(void)
 {
   int a, s, t, v, m, n, k, total = 0;
   int first[10][10], second[10][10], multiple[10][10];
   printf(" Entert the No. of rows and columns for first matrix \n ");
   scanf("%d%d", &m, &n); 
   printf(" Enter your matrix elements : \n ");
   for (a = 0; a < m; a++)
     for (s = 0; s < n; s++)
       scanf("%d", &first[a][s]);
   printf("  Enter the No. of rows and columns for second matrix\n"); 
   scanf(" %d %d", &t, &v);
   if (n != t)
     printf(" Your matrix cannot be multiplied with each other. \n ");
   else
   {
     printf(" Enter your elements for second matrix \n "); 
     for (a = 0; a < t; a++)
       for (s = 0; s < v; s++)
         scanf("%d", &second[a][s] );
     for (a = 0; a < m; a++) {
       for (s = 0; s < v; s++) {
         for (k = 0; k < t; k++) {
         total= total + first[a][k] * second[k][s];
         } 
        multiple[a][s] = total;
        total = 0;
       }
     }
     printf(" The result of matrix multiplication or product of the matrices is: \n "); 
     for (a = 0; a < m; a++) {
       for (s = 0; s < v; s++) 
         printf("%d \t", multiple[a][s] );
       printf(" \n ");
     }
   }
   return 0;
 } 

Output:

Matrix Multiplication in C

Explanation

First, the standard input-output header file must be used, which is the basis for C programming. Then you must declare the main () function, and define it. Inside the main () function scope, you first have to declare some integer variables a, s, t, v, m, n, k, total = 0 followed by some 2D integer arrays (which will serve as matrices in your program) first[10][10], second[10][10], multiple[10][10]. Then use a print statement to inform the user to include a no. of rows and columns of the first matrix with or give data.

Inserting all the elements into your loop needs array one by one, followed by a scanf ().

for (a = 0; a < m; a++)
     for (s = 0; s < n; s++)
       scanf("%d", &first[a][s]); 

This is a nested loop that takes values for "m" number of rows and "n" number of columns, i.e., iterates "m" and "n" number of times in the first[] array to feed the values. Again, use a separate print statement to instruct the user to provide or provide input for a number of second matrix rows and columns.

for (a = 0; a < t; a++)
       for (s = 0; s < v; s++)
         scanf("%d", &second[a][s] ); 

Once more, the same nested loops are used to feed values into the second[] list. Now the condition is checked if the number of columns in the first matrix is equal to that of rows in the second matrix. If this condition satisfies you must write the multiplication logic. For loop this would need to be nested or nested.

for (a = 0; a < m; a++) {
       for (s = 0; s < v; s++) {
         for (k = 0; k < t; k++) {
         total= total + first[a][k] * second[k][s];
         }
        multiple[a][s] = total; 
        total = 0;
       }
     } 

Here the three loops were used which store the first[] and second[] multiplicative value in the tot variable, and this addition of multiplicative values will continue until it passes through all the array values. Place through the measured value of tot in the multiple[] array at the same time, which will place the resulting multiplication. You now need to print the resulting 2D array using loop nesting.

  printf(" The result of matrix multiplication or product of the matrices is: \n "); 
     for (a = 0; a < m; a++) {
       for (s = 0; s < v; s++)
         printf("%d \t", multiple[a][s] );
       printf(" \n ");
     }
   } 
   return 0;
 } 

Related Topics

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

Control statement in C

What is a control statement? A control statement helps us to control the flow of the program. The control statement helps us to execute the program's instructions in a user-defined order....

8 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.

Calendar application in C

Introduction to the calendar application We all are familiar with the calendar. It plays a crucial role in our daily life. We run toward the calendar whenever we want to know...

6 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

What is String Comparison in C

String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison...

4 minutes read.

Distance Vector Routing Protocol Program in c

A distance-vector routing protocol is one of the foremost instructions of routing protocols in pc conversation principle for packet-switched networks. The hyperlink-nation protocol is the alternative foremost class.The Bellman-Ford set...

4 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

Local Labels in C

Anyone who has written programs in the C programming language is required to be familiar with the "go to" and "labels" used in C to navigate between functions. "Local labels"...

4 minutes read.