Skip to main content

C Program to Multiply Two Matrices Using Multi-dimensional Arrays

 

Multiply Matrices by Passing it to a Function

#include <stdio.h>

// function to get matrix elements entered by the user
void getMatrixElements(int matrix[][10], int row, int column) {

   printf("\nEnter elements: \n");

   for (int i = 0; i < row; ++i) {
      for (int j = 0; j < column; ++j) {
         printf("Enter a%d%d: ", i + 1, j + 1);
         scanf("%d", &matrix[i][j]);
      }
   }
}

// function to multiply two matrices
void multiplyMatrices(int first[][10],
                      int second[][10],
                      int result[][10],
                      int r1, int c1, int r2, int c2) {

   // Initializing elements of matrix mult to 0.
   for (int i = 0; i < r1; ++i) {
      for (int j = 0; j < c2; ++j) {
         result[i][j] = 0;
      }
   }

   // Multiplying first and second matrices and storing it in result
   for (int i = 0; i < r1; ++i) {
      for (int j = 0; j < c2; ++j) {
         for (int k = 0; k < c1; ++k) {
            result[i][j] += first[i][k] * second[k][j];
         }
      }
   }
}

// function to display the matrix
void display(int result[][10], int row, int column) {

   printf("\nOutput Matrix:\n");
   for (int i = 0; i < row; ++i) {
      for (int j = 0; j < column; ++j) {
         printf("%d  ", result[i][j]);
         if (j == column - 1)
            printf("\n");
      }
   }
}

int main() {
   int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2;
   printf("Enter rows and column for the first matrix: ");
   scanf("%d %d", &r1, &c1);
   printf("Enter rows and column for the second matrix: ");
   scanf("%d %d", &r2, &c2);

   // Taking input until
   // 1st matrix columns is not equal to 2nd matrix row
   while (c1 != r2) {
      printf("Error! Enter rows and columns again.\n");
      printf("Enter rows and columns for the first matrix: ");
      scanf("%d%d", &r1, &c1);
      printf("Enter rows and columns for the second matrix: ");
      scanf("%d%d", &r2, &c2);
   }

   // get elements of the first matrix
   getMatrixElements(first, r1, c1);

   // get elements of the second matrix
   getMatrixElements(second, r2, c2);

   // multiply two matrices.
   multiplyMatrices(first, second, result, r1, c1, r2, c2);

   // display the result
   display(result, r1, c2);

   return 0;
}

Output

Enter rows and column for the first matrix: 2
3
Enter rows and column for the second matrix: 3
2

Enter elements:
Enter a11: 2  
Enter a12: -3
Enter a13: 4
Enter a21: 53
Enter a22: 3
Enter a23: 5

Enter elements:
Enter a11: 3
Enter a12: 3
Enter a21: 5
Enter a22: 0
Enter a31: -3
Enter a32: 4

Output Matrix:
-21  22
159  179

Comments

Popular posts from this blog

C Program to Calculate Difference Between Two Time Periods

  Calculate Difference Between Two Time Periods # include <stdio.h> struct TIME { int seconds; int minutes; int hours; }; void differenceBetweenTimePeriod (struct TIME t1, struct TIME t2, struct TIME *diff) ; int main () { struct TIME startTime , stopTime , diff ; printf ( "Enter the start time. \n" ); printf ( "Enter hours, minutes and seconds: " ); scanf ( "%d %d %d" , &startTime.hours, &startTime.minutes, &startTime.seconds); printf ( "Enter the stop time. \n" ); printf ( "Enter hours, minutes and seconds: " ); scanf ( "%d %d %d" , &stopTime.hours, &stopTime.minutes, &stopTime.seconds); // Difference between start and stop time differenceBetweenTimePeriod(startTime, stopTime, &diff); printf ( "\nTime Difference: %d:%d:%d - ...

What is Cloud computing?

Cloud computing refers to the delivery of computing resources and services over the internet, allowing users to access and use applications, storage, and processing power without the need for local infrastructure or hardware. It involves the use of remote servers, typically hosted in data centers, to store, manage, and process data instead of relying on a local computer or server. In cloud computing, users can access a wide range of services and resources on-demand, which are usually provided by a cloud service provider (CSP). These services can include infrastructure resources (such as virtual machines, storage, and networking), platforms for developing and deploying applications, and software applications that can be accessed and used via the internet. Cloud computing offers several advantages over traditional on-premises computing models. It allows for scalability, where users can easily scale up or down their resource usage based on their needs, without having to invest in and mana...

C Program to Sort Elements in Lexicographical Order (Dictionary Order)

  Sort strings in the dictionary order # include <stdio.h> # include <string.h> int main () { char str[ 5 ][ 50 ], temp[ 50 ]; printf ( "Enter 5 words: " ); // Getting strings input for ( int i = 0 ; i < 5 ; ++i) { fgets(str[i], sizeof (str[i]), stdin ); } // storing strings in the lexicographical order for ( int i = 0 ; i < 5 ; ++i) { for ( int j = i + 1 ; j < 5 ; ++j) { // swapping strings if they are not in the lexicographical order if ( strcmp (str[i], str[j]) > 0 ) { strcpy (temp, str[i]); strcpy (str[i], str[j]); strcpy (str[j], temp); } } } printf ( "\nIn the lexicographical order: \n" ); for ( int i = 0 ; i < 5 ; ++i) { fputs (str[i], stdout ); } return 0 ; } Output Enter 5 words: R programming JavaScript Java C programming C++ programming In the lexicographical order: C programming C+...