```
#include <iostream>
using namespace std;
void enterData(double firstMatrix[][10], double secondMatrix[][10], int rowSecondFirst, int columnCountFirst, int rowCountSecond, int columnCountSecond);
void multiplyMatrices(double firstMatrix[][10], double secondMatrix[][10], double result[][10], int rowCountFirst, int columnCountFirst, int rowCountSecond, int columnCountSecond);
void display(double mult[][10], double rowFirst, double columnSecond);
int i, j, k;
int main()
{
double firstMatrix[10][10], secondMatrix[10][10], mult[10][10], rowCountFirst, columnCountFirst, rowCountSecond, columnCountSecond;
cout << "Enter rows and column for first matrix: ";
cin >> rowCountFirst >> columnCountFirst;
cout << "Enter rows and column for second matrix: ";
cin >> rowCountSecond >> columnCountSecond;
// If column of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again.
while (columnCountFirst != rowCountSecond)
{
cout << "Error! column of first matrix not equal to row of second." << endl;
cout << "Enter rows and column for first matrix: ";
cin >> rowCountFirst >> columnCountFirst;
cout << "Enter rows and column for second matrix: ";
cin >> rowCountSecond >> columnCountSecond;
}
// Function to take matrices data
enterData(firstMatrix, secondMatrix, rowCountFirst, columnCountFirst, rowCountSecond, columnCountSecond);
// Function to multiply two matrices.
multiplyMatrices(firstMatrix, secondMatrix, mult, rowCountFirst, columnCountFirst, rowCountSecond, columnCountSecond);
// Function to display resultant matrix after multiplication.
display(mult, rowCountFirst, columnCountSecond);
system("pause");
return 0;
}
void enterData(double firstMatrix[][10], double secondMatrix[][10], int rowCountFirst, int columnCountFirst, int rowCountSecond, int columnCountSecond)
{
cout << endl << "Enter elements of matrix 1:" << endl;
for (i = 0; i < rowCountFirst; ++i)
{
for (j = 0; j < columnCountFirst; ++j)
{
cout << "Enter elements a " << i + 1 << j + 1 << ": ";
cin >> firstMatrix[i][j];
}
}
cout << endl << "Enter elements of matrix 2:" << endl;
for (i = 0; i < rowCountSecond; ++i)
{
for (j = 0; j < columnCountSecond; ++j)
{
cout << "Enter elements b" << i + 1 << j + 1 << ": ";
cin >> secondMatrix[i][j];
}
}
}
void multiplyMatrices(double firstMatrix[][10], double secondMatrix[][10], double mult[][10], int rowCountFirst, int columnCountFirst, int rowCountSecond, int columnCountSecond)
{
// Initializing elements of matrix mult to 0.
for (i = 0; i < rowCountFirst; ++i)
{
for (j = 0; j < columnCountSecond; ++j)
{
mult[i][j] = 0;
}
}
// Multiplying matrix firstMatrix and secondMatrix and storing in array mult.
for (i = 0; i < rowCountFirst; ++i)
{
for (j = 0; j < columnCountSecond; ++j)
{
for (k = 0; k<columnCountFirst; ++k)
{
mult[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
}
void display(double mult[][10], double rowCountFirst, double columnCountSecond)
{
cout << endl << "The result Matrix is:" << endl;
for (i = 0; i < rowCountFirst; ++i)
{
for (j = 0; j < columnCountSecond; ++j)
{
cout << mult[i][j] << " ";
if (j == columnCountSecond - 1)
cout << endl << endl;
}
}
}
```

## Recommended Answers

Jump to PostA few points...

- You posted here too. https://www.daniweb.com/programming/software-development/code/355645/optimizing-matrix-multiplication That's bad for a number of reasons. One, you're posting the same exact question in two threads. Two, the other thread was a code SNIPPET, not a normal thread. Responses in a code snippet thread should be highly relevant to …

## All 3 Replies

AssertNull
1,094
Practically a Posting Shark

Sam_31
0
Newbie Poster

AssertNull
1,094
Practically a Posting Shark

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.