Matrix Multiplication - C Program

Submitted by: 
Language: 
Visitors have accessed this post 1250 times.

A program that can be used to multiply two matrices. It uses the following algorithm:

Step 1: Make sure that the the number of columns in the 1st one equals the number of rows in the 2nd one. (The pre-requisite to be able to multiply)

Step 2: Multiply the elements of each row of the first matrix by the elements of each column in the second matrix.

Step 3: Add the products.

  1. #include <stdio.h>
  2.  
  3.  
  4.  
  5. //Variable declaration and initialization
  6. int first_matrix_rows, first_matrix_columns;
  7. int second_matrix_rows, second_matrix_columns;
  8. int matrix_rows_counter, matrix_columns_counter;
  9. int matrix_calculation_counter;
  10. int matrix_calculation_holder = 0;
  11. int first_matrix_array[20][20];
  12. int second_matrix_array[20][20];
  13. int product_matrix_array[20][20];
  14.  
  15. int main(void)//Main method
  16. {
  17.  
  18. //Prompt user to enter the rows of the first matrix and its capture and storage
  19. printf("Please enter the number of rows of the first matrix\n");
  20. scanf("%d", &first_matrix_rows);
  21.  
  22. //Prompt user to enter the columns of the first matrix and its capture and storage
  23. printf("Please enter the number of columns of the first matrix\n");
  24. scanf("%d", &first_matrix_columns);
  25.  
  26. //Prompt user to enter the elements of the first matrix
  27. printf("Enter the elements of the first matrix\n");
  28.  
  29. //Capture and storage of the elements of the first matrix through and array called first_matrix_array[][]
  30. for (matrix_rows_counter = 0 ; matrix_rows_counter < first_matrix_rows ; matrix_rows_counter++ )
  31. { for ( matrix_columns_counter = 0 ; matrix_columns_counter < first_matrix_columns ; matrix_columns_counter++ )
  32. {
  33. scanf("%d", &first_matrix_array[matrix_rows_counter][matrix_columns_counter]);
  34. }
  35. }
  36.  
  37. //Prompt user to enter the rows of the second matrix and its capture and storage
  38. printf("Enter the number of rows of second matrix\n");
  39. scanf("%d", &second_matrix_rows);
  40.  
  41. //Prompt user to enter the columns of the second matrix and its capture and storage
  42. printf("Enter the number of columns of second matrix\n");
  43. scanf("%d", &second_matrix_rows, &second_matrix_columns);
  44.  
  45. //Test the prerequisite for matrix multiplication. Make sure that the the number of columns in the 1st one equals the number of rows in the 2nd one.
  46. if ( first_matrix_columns != second_matrix_rows )
  47. printf("Matrix 1 and 2 cannot be multiplied together because the number of columns in 1 does not equal the number of rows in 2. In this case, the multiplication of these two matrices is not defined. \n");
  48. else
  49. {
  50. //Prompt user to enter the elements of the second matrix
  51. printf("Enter the elements of second matrix\n");
  52.  
  53. //Capture and storage of the elements of the second matrix through and array called first_matrix_array[][]
  54. for ( matrix_rows_counter = 0 ; matrix_rows_counter < second_matrix_rows ; matrix_rows_counter++ )
  55. for ( matrix_columns_counter = 0 ; matrix_columns_counter < second_matrix_columns ; matrix_columns_counter++ )
  56. scanf("%d", &second_matrix_array[matrix_rows_counter][matrix_columns_counter]);
  57.  
  58. //Multiply the elements of the matrix, row by column, one after the other and storage into a third matrix known as product_matrix_array[][]
  59. for ( matrix_rows_counter = 0 ; matrix_rows_counter < first_matrix_rows ; matrix_rows_counter++ )
  60. {
  61. for ( matrix_columns_counter = 0 ; matrix_columns_counter < second_matrix_columns ; matrix_columns_counter++ )
  62. {
  63. for ( matrix_calculation_counter = 0 ; matrix_calculation_counter< second_matrix_rows ; matrix_calculation_counter++ )
  64. {
  65. matrix_calculation_holder = matrix_calculation_holder + first_matrix_array[matrix_rows_counter][matrix_calculation_counter]*second_matrix_array[matrix_calculation_counter][matrix_columns_counter];
  66. }
  67.  
  68. product_matrix_array[matrix_rows_counter][matrix_columns_counter] = matrix_calculation_holder;
  69. matrix_calculation_holder = 0;
  70. }
  71. }
  72.  
  73. //A simple output segment preceeding the product of the multiplication
  74. printf("The product of the matrix multiplication is:\n");
  75.  
  76. //Display of the elements of the third array named product_matrix_array[][]
  77. for ( matrix_rows_counter = 0 ; matrix_rows_counter < first_matrix_rows ; matrix_rows_counter++ )
  78. {
  79. for ( matrix_columns_counter = 0 ; matrix_columns_counter < second_matrix_columns ; matrix_columns_counter++ )
  80. printf("%d\t", product_matrix_array[matrix_rows_counter][matrix_columns_counter]);
  81.  
  82. printf("\n");
  83. }
  84. }
  85.  
  86. return 0;
  87. }
  88.  
  89.  
  90.  
  91. <c>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.