×

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

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

3 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

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

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

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

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

Recursion in C

Recursion is a programming technique that allows the programmer to call the function within the same function. The function which calls the same function is known as recursive function but a...

1 minute read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 minutes read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

Double Specifier in C

What is a double specifier? The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of...

3 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute 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.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

What is algorithm in C?

What is an algorithm? In computer programming languages, an algorithm is set of statement that solves a particular problem. In C, first step of every algorithm is Start and last step of...

3 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.