Matrix Multiplication - C Program
Submitted by davidwachira on Sunday, August 4, 2013 - 20:07.
Language
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.
- #include <stdio.h>
- //Variable declaration and initialization
- int first_matrix_rows, first_matrix_columns;
- int second_matrix_rows, second_matrix_columns;
- int matrix_rows_counter, matrix_columns_counter;
- int matrix_calculation_counter;
- int matrix_calculation_holder = 0;
- int first_matrix_array[20][20];
- int second_matrix_array[20][20];
- int product_matrix_array[20][20];
- int main(void)//Main method
- {
- //Prompt user to enter the rows of the first matrix and its capture and storage
- //Prompt user to enter the columns of the first matrix and its capture and storage
- //Prompt user to enter the elements of the first matrix
- //Capture and storage of the elements of the first matrix through and array called first_matrix_array[][]
- for (matrix_rows_counter = 0 ; matrix_rows_counter < first_matrix_rows ; matrix_rows_counter++ )
- { for ( matrix_columns_counter = 0 ; matrix_columns_counter < first_matrix_columns ; matrix_columns_counter++ )
- {
- }
- }
- //Prompt user to enter the rows of the second matrix and its capture and storage
- //Prompt user to enter the columns of the second matrix and its capture and storage
- //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.
- if ( first_matrix_columns != second_matrix_rows )
- 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");
- else
- {
- //Prompt user to enter the elements of the second matrix
- //Capture and storage of the elements of the second matrix through and array called first_matrix_array[][]
- for ( matrix_rows_counter = 0 ; matrix_rows_counter < second_matrix_rows ; matrix_rows_counter++ )
- for ( matrix_columns_counter = 0 ; matrix_columns_counter < second_matrix_columns ; matrix_columns_counter++ )
- //Multiply the elements of the matrix, row by column, one after the other and storage into a third matrix known as product_matrix_array[][]
- for ( matrix_rows_counter = 0 ; matrix_rows_counter < first_matrix_rows ; matrix_rows_counter++ )
- {
- for ( matrix_columns_counter = 0 ; matrix_columns_counter < second_matrix_columns ; matrix_columns_counter++ )
- {
- for ( matrix_calculation_counter = 0 ; matrix_calculation_counter< second_matrix_rows ; matrix_calculation_counter++ )
- {
- matrix_calculation_holder = matrix_calculation_holder + first_matrix_array[matrix_rows_counter][matrix_calculation_counter]*second_matrix_array[matrix_calculation_counter][matrix_columns_counter];
- }
- product_matrix_array[matrix_rows_counter][matrix_columns_counter] = matrix_calculation_holder;
- matrix_calculation_holder = 0;
- }
- }
- //A simple output segment preceeding the product of the multiplication
- //Display of the elements of the third array named product_matrix_array[][]
- for ( matrix_rows_counter = 0 ; matrix_rows_counter < first_matrix_rows ; matrix_rows_counter++ )
- {
- for ( matrix_columns_counter = 0 ; matrix_columns_counter < second_matrix_columns ; matrix_columns_counter++ )
- }
- }
- return 0;
- }
- <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
- 218 views