Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,970 questions

51,912 answers

573 users

How to multiply two matrices (matrix) in C++

2 Answers

0 votes
#include <iostream>
 
#define COLS1 3
#define COLS2 2
 
void multiplyMatrices(const int matrix1[][COLS1], const int matrix2[][COLS2],
                      const int rows1, int result[][COLS2]) {
    for (int i = 0; i < rows1; i++) {
        for (int j = 0; j < COLS2; j++) {
            result[i][j] = 0; // initialize
            for (int k = 0; k < COLS1; k++) {
                result[i][j] += matrix1[i][k] * matrix2[k][j];
                std::cout << "result[" << i << "][" << j << "] += matrix1[" << i 
                          << "][" << k << "] * matrix2[" << k << "][" << j << "]\n";
            }
             
            std::cout << "\n";
        }
    }
}
 
int main() {
    int matrix1[][COLS1] = { {4, 2, 4},
                             {8, 3, 1} };
    int matrix2[][COLS2] = { {3, 5},
                             {2, 8},
                             {7, 9} };
    int result[][COLS2] = { {0, 0},
                            {0, 0} };
 
    int rows1 = sizeof(matrix1) / sizeof(matrix1[0]);

    multiplyMatrices(matrix1, matrix2, rows1, result);
 
    std::cout << "Final result:\n";
    for (int i = 0; i < rows1; i++) {
        for (int j = 0; j < COLS2; j++)
            std::cout << result[i][j] << " ";
        std::cout << "\n";
    }
}

 
  
/*
run:
  
result[0][0] += matrix1[0][0] * matrix2[0][0]
result[0][0] += matrix1[0][1] * matrix2[1][0]
result[0][0] += matrix1[0][2] * matrix2[2][0]
 
result[0][1] += matrix1[0][0] * matrix2[0][1]
result[0][1] += matrix1[0][1] * matrix2[1][1]
result[0][1] += matrix1[0][2] * matrix2[2][1]
 
result[1][0] += matrix1[1][0] * matrix2[0][0]
result[1][0] += matrix1[1][1] * matrix2[1][0]
result[1][0] += matrix1[1][2] * matrix2[2][0]
 
result[1][1] += matrix1[1][0] * matrix2[0][1]
result[1][1] += matrix1[1][1] * matrix2[1][1]
result[1][1] += matrix1[1][2] * matrix2[2][1]
 
Final result:
44 72 
37 73 
 
*/

 



answered May 7, 2021 by avibootz
edited Jul 10, 2025 by avibootz
0 votes
#include <iostream>

#define COLS1 3
#define COLS2 2

// If matrix size is constant
void multiplyMatrices2x3By3x2(const int matrix1[][COLS1], const int matrix2[][COLS2],
                              const int rows1, int result[][COLS2]) {
   for (unsigned int i = 0; i < rows1; i++) { // i (rows) = 0, 1
        for (unsigned int j = 0; j < COLS2; j++) { // j (cols) = 0, 1
             result[i][j] = matrix1[i][0] * matrix2[0][j] +
                            matrix1[i][1] * matrix2[1][j] +
                            matrix1[i][2] * matrix2[2][j];
        }
    }
}

int main() {
    int matrix1[][COLS1] = { {1, 2, 3},
                             {4, 5, 6} };
    int matrix2[][COLS2] = { {10, 11},
                             {20, 21},
                             {30, 31} };
    int result[][COLS2] = { {0, 0},
                            {0, 0} };

    int rows1 = sizeof(matrix1) / sizeof(matrix1[0]);

    multiplyMatrices2x3By3x2(matrix1, matrix2, rows1, result);

    std::cout << "Final result:\n";
    for (int i = 0; i < rows1; i++) {
        for (int j = 0; j < COLS2; j++)
            std::cout << result[i][j] << " ";
        std::cout << "\n";
    }
}


 
/*
run:
 
Final result:
140 146 
320 335 

*/

 



answered Jul 10, 2025 by avibootz
edited Jul 10, 2025 by avibootz
...