×

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

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

How to install a C compiler in windows 10-64bit

Many C compilers can be installed on your pc. The most downloaded compilers include GCC, Turbo C++, Visual Studio, etc. This article includes the installation of the Turbo C++ compiler in...

6 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

function pointer as argument in C

Pointers are considered difficult to understand for beginners but pointers can be made to work if you fiddle with them long enough. So, let’s understand this step by step. What are...

3 minutes read.

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used...

4 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

Find Day from Day in C Without using function

Introduction: In the given article, I find daily in C without using functions. It takes 365 days for the earth to revolve around the sun. It will be close to...

3 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

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

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as...

4 minutes read.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

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

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.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

C –Structure

C structure is a collection of different types of data that is grouped together. It is used to represent records.Structkeyword is used to create a structure. Each element of a...

1 minute read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

3 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.