#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;
        }
    }
}

A few points...

  1. 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 the code snippet or the concept of the code snippet. That's true for all threads, but it's extra true for a Code Snippet thread. Three, the thread was from five years ago. You should have a very good reason to revive such a thread, and again, even moreso since it's a Code Snippet thread. Better to start a new thread, as you have here.
  2. For THIS thread, much more detail is needed. Optimize how? What's the problem? Don't make us guess.
  3. You have to show effort. Yes, you've posted code, but that's all you've done. we don't know where this code came from, what it's supposed to do (though we can guess pretty darn fast), and you're forcing us to enter in a matrix manually. You should provide that and make it easy if you want help. Who's going to enter in 100 values manually just to try someone's code out?

Optimize for both memory and quick execution for example replacing cin with getchar_unlocked()

Regarding replacing cin with getchar_unlocked, I know next to nothing about thread safety and how that affects cin/cout/stdin/stdou, but my feeling is that since getchar_unlocked is NOT thread-safe, there should be a good reason to use it instead of getchar. If not, why potentially complicate things? I see no good reason to do so here. You are entering doubles. getchar and cin >> work way faster than people can type in my experience. Now if you get to a spot where no one is typing anymore and it's slowing things down, worry about it then. But until then... if it ain't broke, don't fix it. cin >> works just fine in my experience.

Regarding memory, you might consider what happens if In type in a number larger than 10 for the size. You reserve memory statically for 10x10 matrices. An array index of 10 or more could be an invalid index, so either test for a valid size or try creating the memory dynamically using the new command...

http://www.cplusplus.com/doc/tutorial/dynamic/

Also realize that a 10x10 matrix 2-D matrix can be treated like 100 a element single dimensional matrix.

https://www.tutorialspoint.com/cplusplus/cpp_multi_dimensional_arrays.htm

For efficiency, you don't necessarily need to loop through the array using TWO loop control variables (i and j). You can potentially do a few tricks and use less code, make things a bit quicke, etc. In particular, you can efficiently initialize your arrays to 0 using the memset command.

http://www.cplusplus.com/reference/cstring/memset/

However, my personal preference is often to keep things in the multiple loops. It's easier to read and write, which is always good. Unless you're on an embedded system or there is some other squeeze on resources, I think far too much effort is spent "optimizing" this stuff. Memory is cheap, processors are fast, and typing is slow.

As far as the actual matrix multiplication goes, that's what takes the most processing power I would imagine. There are lots of good articles out there about when/how/why to do that. That's not a field that I'm knowledgeable in, so I can't offer anything on that aspect.

Be a part of the DaniWeb community

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