C Program

C Program for multiplication of two matrices

c program
#include <stdio.h>

#define MAX 10 // Maximum size of the matrices

void multiplyMatrices(int first[MAX][MAX], int second[MAX][MAX], int result[MAX][MAX], int rowFirst, int columnFirst, int rowSecond, int columnSecond);

int main() {
int first[MAX][MAX], second[MAX][MAX], result[MAX][MAX];
int rowFirst, columnFirst, rowSecond, columnSecond;

// Input for the first matrix
printf("Enter rows and columns for first matrix: ");
scanf("%d %d", &rowFirst, &columnFirst);

printf("Enter elements of the first matrix:\n");
for (int i = 0; i < rowFirst; i++) {
for (int j = 0; j < columnFirst; j++) {
scanf("%d", &first[i][j]);
}
}

// Input for the second matrix
printf("Enter rows and columns for second matrix: ");
scanf("%d %d", &rowSecond, &columnSecond);

// Check if multiplication is possible
if (columnFirst != rowSecond) {
printf("Matrix multiplication not possible.\n");
return 0;
}

printf("Enter elements of the second matrix:\n");
for (int i = 0; i < rowSecond; i++) {
for (int j = 0; j < columnSecond; j++) {
scanf("%d", &second[i][j]);
}
}

// Multiply matrices
multiplyMatrices(first, second, result, rowFirst, columnFirst, rowSecond, columnSecond);

// Display the result
printf("Resultant Matrix:\n");
for (int i = 0; i < rowFirst; i++) {
for (int j = 0; j < columnSecond; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}

return 0;
}

void multiplyMatrices(int first[MAX][MAX], int second[MAX][MAX], int result[MAX][MAX], int rowFirst, int columnFirst, int rowSecond, int columnSecond) {
// Initialize the result matrix
for (int i = 0; i < rowFirst; i++) {
for (int j = 0; j < columnSecond; j++) {
result[i][j] = 0; // Set initial value to zero
}
}

// Perform multiplication
for (int i = 0; i < rowFirst; i++) {
for (int j = 0; j < columnSecond; j++) {
for (int k = 0; k < columnFirst; k++) {
result[i][j] += first[i][k] * second[k][j]; // Multiply and accumulate
}
}
}
}
  1. Header File Inclusion

    #include <stdio.h>
    

    This includes the standard input-output library needed for functions like printf and scanf

  2. Macro Definition

    #define MAX 10
    

    This defines a constant MAX for the maximum size of the matrices. You can adjust this value as needed.

  3. Function Declaration

    void multiplyMatrices(int first[MAX][MAX], int second[MAX][MAX], int result[MAX][MAX], int rowFirst, int columnFirst, int rowSecond, int columnSecond);

    This declares the function that will perform the multiplication of two matrices.

  4. Main Function

    • Matrix Declaration and Initialization

      int first[MAX][MAX], second[MAX][MAX], result[MAX][MAX];

      Three matrices are declared: first, second, and result to store the multiplication output.

    • Input for First Matrix

      printf("Enter rows and columns for first matrix: ");
      scanf("%d %d", &rowFirst, &columnFirst);

      The user is prompted to enter the dimensions and elements of the first matrix.

    • Input for Second Matrix

      printf("Enter rows and columns for second matrix: ");
      scanf("%d %d", &rowSecond, &columnSecond);

      Similar input is taken for the second matrix.

    • Matrix Multiplication Feasibility Check

      if (columnFirst != rowSecond) {
      printf("Matrix multiplication not possible.\n");
      return 0;
      }

      This checks if the multiplication is possible. The number of columns in the first matrix must equal the number of rows in the second.

    • Calling the Multiplication Function

      multiplyMatrices(first, second, result, rowFirst, columnFirst, rowSecond, columnSecond);

      This calls the multiplyMatrices function to perform the multiplication.

    • Displaying the Result

      printf("Resultant Matrix:\n");
      for (int i = 0; i < rowFirst; i++) {
      for (int j = 0; j < columnSecond; j++) {
      printf("%d ", result[i][j]);
      }
      printf("\n");
      }

      Finally, the resulting matrix is printed.

  5. Matrix Multiplication Function

    void multiplyMatrices(int first[MAX][MAX], int second[MAX][MAX], int result[MAX][MAX], int rowFirst, int columnFirst, int rowSecond, int columnSecond) {
    // Initialize the result matrix
    for (int i = 0; i < rowFirst; i++) {
    for (int j = 0; j < columnSecond; j++) {
    result[i][j] = 0;
    }
    }

    // Perform multiplication
    for (int i = 0; i < rowFirst; i++) {
    for (int j = 0; j < columnSecond; j++) {
    for (int k = 0; k < columnFirst; k++) {
    result[i][j] += first[i][k] * second[k][j];
    }
    }
    }
    }

    • Initialization of Result Matrix Each element of the result matrix is initialized to zero.

    • Nested Loops for Multiplication The outer two loops iterate through each cell of the resulting matrix. The innermost loop performs the dot product of the corresponding row of the first matrix and the column of the second matrix.

Example Input and Output

Input:

Enter rows and columns for first matrix: 2 3
Enter elements of the first matrix:
1 2 3
4 5 6
Enter rows and columns for second matrix: 3 2
Enter elements of the second matrix:
7 8
9 10
11 12

Output:

Resultant Matrix:
58 64
139 154

Team Educate

About Author

Leave a comment

Your email address will not be published. Required fields are marked *

You may also like

C program to test whether a given identifier is valid or not
C Program

C Program to Test Whether a Given Identifier is Valid or Not

Rules for Valid Identifiers in C: The identifier must begin with a letter or an underscore (_). It cannot start
C program to test whether a given identifier is valid or not
C Program Compiler Design

C program to implement simple code generator

ALGORITHM:1. Start2. Get address code sequence.3. Determine current location of 3 using address (for 1st operand).4. If current location not