×

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

How to use sine() function in C

What is Function? The function is a set of statements. It takes input and performs some computation to produce output. The process is a set of codes only achieved when it is...

3 minutes read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

Star Program in C Language

Star Program in C Star patterns are a sequence of * or any other character used to construct any pattern or any like-square geometric form, triangle, hollow square, pyramid, rhombus, etc.  Many programmers worldwide highly...

4 minutes read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Use of fflush(stdin) in C

Use of fflush(stdin) in C Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into...

2 minutes read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute....

4 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 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.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 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.

Pascal Triangle in C

The pascal triangle in c is an array of binomial coefficients in triangular form. Here the nth row contains the binomial coefficient of ncr.In a pascal triangle, every number is...

2 minutes read.