• We just launched and are currently in beta. Join us as we build and grow the community.

Matrix Multiplication - C Program

kittyno9

Core Web Vitals Expert
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
85
Likes
29
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. //Variable declaration and initialization
  3. int

    first_matrix_rows,

    first_matrix_columns;
  4. int

    second_matrix_rows,

    second_matrix_columns;
  5. int

    matrix_rows_counter,

    matrix_columns_counter;
  6. int

    matrix_calculation_counter;
  7. int

    matrix_calculation_holder =

    0

    ;
  8. int

    first_matrix_array[

    20

    ]

    [

    20

    ]

    ;
  9. int

    second_matrix_array[

    20

    ]

    [

    20

    ]

    ;
  10. int

    product_matrix_array[

    20

    ]

    [

    20

    ]

    ;

  11. int

    main(

    void

    )

    //Main method
  12. {

  13. //Prompt user to enter the rows of the first matrix and its capture and storage
  14. printf

    (

    "Please enter the number of rows of the first matrix\n

    "

    )

    ;
  15. scanf

    (

    "%d"

    ,

    &

    first_matrix_rows)

    ;

  16. //Prompt user to enter the columns of the first matrix and its capture and storage
  17. printf

    (

    "Please enter the number of columns of the first matrix\n

    "

    )

    ;
  18. scanf

    (

    "%d"

    ,

    &

    first_matrix_columns)

    ;

  19. //Prompt user to enter the elements of the first matrix
  20. printf

    (

    "Enter the elements of the first matrix\n

    "

    )

    ;

  21. //Capture and storage of the elements of the first matrix through and array called first_matrix_array[][]
  22. for

    (

    matrix_rows_counter =

    0

    ;

    matrix_rows_counter <

    first_matrix_rows ;

    matrix_rows_counter++

    )
  23. {

    for

    (

    matrix_columns_counter =

    0

    ;

    matrix_columns_counter <

    first_matrix_columns ;

    matrix_columns_counter++

    )
  24. {
  25. scanf

    (

    "%d"

    ,

    &

    first_matrix_array[

    matrix_rows_counter]

    [

    matrix_columns_counter]

    )

    ;
  26. }
  27. }

  28. //Prompt user to enter the rows of the second matrix and its capture and storage
  29. printf

    (

    "Enter the number of rows of second matrix\n

    "

    )

    ;
  30. scanf

    (

    "%d"

    ,

    &

    second_matrix_rows)

    ;

  31. //Prompt user to enter the columns of the second matrix and its capture and storage
  32. printf

    (

    "Enter the number of columns of second matrix\n

    "

    )

    ;
  33. scanf

    (

    "%d"

    ,

    &

    second_matrix_rows,

    &

    second_matrix_columns)

    ;

  34. //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.
  35. if

    (

    first_matrix_columns !=

    second_matrix_rows )
  36. 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

    "

    )

    ;
  37. else
  38. {
  39. //Prompt user to enter the elements of the second matrix
  40. printf

    (

    "Enter the elements of second matrix\n

    "

    )

    ;

  41. //Capture and storage of the elements of the second matrix through and array called first_matrix_array[][]
  42. for

    (

    matrix_rows_counter =

    0

    ;

    matrix_rows_counter <

    second_matrix_rows ;

    matrix_rows_counter++

    )
  43. for

    (

    matrix_columns_counter =

    0

    ;

    matrix_columns_counter <

    second_matrix_columns ;

    matrix_columns_counter++

    )
  44. scanf

    (

    "%d"

    ,

    &

    second_matrix_array[

    matrix_rows_counter]

    [

    matrix_columns_counter]

    )

    ;

  45. //Multiply the elements of the matrix, row by column, one after the other and storage into a third matrix known as product_matrix_array[][]
  46. for

    (

    matrix_rows_counter =

    0

    ;

    matrix_rows_counter <

    first_matrix_rows ;

    matrix_rows_counter++

    )
  47. {
  48. for

    (

    matrix_columns_counter =

    0

    ;

    matrix_columns_counter <

    second_matrix_columns ;

    matrix_columns_counter++

    )
  49. {
  50. for

    (

    matrix_calculation_counter =

    0

    ;

    matrix_calculation_counter<

    second_matrix_rows ;

    matrix_calculation_counter++

    )
  51. {
  52. matrix_calculation_holder =

    matrix_calculation_holder +

    first_matrix_array[

    matrix_rows_counter]

    [

    matrix_calculation_counter]

    *

    second_matrix_array[

    matrix_calculation_counter]

    [

    matrix_columns_counter]

    ;
  53. }

  54. product_matrix_array[

    matrix_rows_counter]

    [

    matrix_columns_counter]

    =

    matrix_calculation_holder;
  55. matrix_calculation_holder =

    0

    ;
  56. }
  57. }

  58. //A simple output segment preceeding the product of the multiplication
  59. printf

    (

    "The product of the matrix multiplication is:\n

    "

    )

    ;

  60. //Display of the elements of the third array named product_matrix_array[][]
  61. for

    (

    matrix_rows_counter =

    0

    ;

    matrix_rows_counter <

    first_matrix_rows ;

    matrix_rows_counter++

    )
  62. {
  63. for

    (

    matrix_columns_counter =

    0

    ;

    matrix_columns_counter <

    second_matrix_columns ;

    matrix_columns_counter++

    )
  64. printf

    (

    "%d\t

    "

    ,

    product_matrix_array[

    matrix_rows_counter]

    [

    matrix_columns_counter]

    )

    ;

  65. printf

    (

    "\n

    "

    )

    ;
  66. }
  67. }

  68. return

    0

    ;
  69. }



  70. <

    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.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

440,010

316,559

316,568

Top