×

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

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.

FIFO Example in the C Language

FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is...

3 minutes read.

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

Range of int in C

In this session we know about the rage of the int in C language with the suitable examples and the programs. Firstly, we can learn the range of int: The int is...

3 minutes read.

Conditional Operator in C

In C programming language, the conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement. The syntax of conditional operator in C is...

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

Pointer in C

Pointer is a variable that is used to contain the address of another variable. We can easily create a pointer in C language. Example: int *ptr Symbol Description & (ampersand sign) Address of an operator...

1 minute read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

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.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.