Back to C++
2026-04-276 min read

Example: Multiply two matrices without using functions (C++)

Learn Example: Multiply two matrices without using functions (C++) step by step with clear examples and exercises.

Why This Matters

In this full guide on multiplying two matrices in C++ without functions, we aim to provide you with a deep understanding of matrix multiplication and its practical applications. By learning this technique, you will gain valuable skills in memory management, performance optimization, and debugging complex programs involving matrices. Additionally, mastering manual matrix multiplication can help you tackle interview questions or real-world programming challenges that require explicit matrix manipulation.

Prerequisites

Before diving into the core concept, ensure you have a solid grasp of the following topics:

  1. C++ basics, including variables, loops, and arrays
  2. Memory allocation and deallocation in C++
  3. Understanding of matrix structure and operations
  4. Familiarity with pointers and multi-dimensional arrays
  5. Knowledge of basic linear algebra concepts (matrix dimensions, transposition)

Core Concept

To multiply two matrices manually in C++, we will use multi-dimensional arrays to represent our matrices and perform the multiplication operation step by step. Here's a breakdown of the process:

  1. Allocate memory for both matrices using dynamic memory allocation (new[]).
  2. Read input values for each matrix and store them in the allocated memory.
  3. Perform the multiplication operation element-wise, following the rules of matrix multiplication.
  4. Deallocate memory for both matrices when finished to avoid memory leaks.

Matrix Multiplication Rules

Matrix multiplication is performed by multiplying each element in a row of the first matrix with the corresponding element in a column of the second matrix and summing the results. This operation is repeated for every row and column in both matrices.

Special Cases: Transposition and Identity Matrices

  1. Transposition: To transpose a matrix, swap its rows and columns. In C++, you can create a new matrix with swapped dimensions and copy the elements from the original matrix accordingly.
  2. Identity Matrix: An identity matrix is a square matrix where all diagonal elements are 1 and the rest are 0. You can create an identity matrix by initializing the diagonal elements to 1 and setting all other elements to 0.

Worked Example

Let's consider two 3x3 matrices:

Matrix A:

1 2 3

4 5 6

7 8 9

Matrix B:

10 11 12

13 14 15

16 17 18

To multiply these matrices manually, follow the steps below:

  1. Allocate memory for both matrices:
int rowsA = 3;
int colsA = 3;
int rowsB = 3;
int colsB = 3;

int** A = new int* [rowsA];
for (int i = 0; i < rowsA; ++i) {
A[i] = new int[colsA];
}

int** B = new int* [rowsB];
for (int j = 0; j < rowsB; ++j) {
B[j] = new int[colsB];
}
  1. Read the input values for both matrices:
// Input matrix A values
A[0][0] = 1; A[0][1] = 2; A[0][2] = 3;
A[1][0] = 4; A[1][1] = 5; A[1][2] = 6;
A[2][0] = 7; A[2][1] = 8; A[2][2] = 9;

// Input matrix B values
B[0][0] = 10; B[0][1] = 11; B[0][2] = 12;
B[1][0] = 13; B[1][1] = 14; B[1][2] = 15;
B[2][0] = 16; B[2][1] = 17; B[2][2] = 18;
  1. Perform the multiplication operation:
int** result = new int* [rowsA];
for (int i = 0; i < rowsA; ++i) {
result[i] = new int[colsB];
}

// Multiply matrices A and B and store the result
for (int i = 0; i < rowsA; ++i) {
for (int j = 0; j < colsB; ++j) {
result[i][j] = 0;
for (int k = 0; k < colsA; ++k) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
  1. Print the resulting matrix:
// Print the multiplication result
for (int i = 0; i < rowsA; ++i) {
for (int j = 0; j < colsB; ++j) {
cout << result[i][j] << " ";
}
cout << endl;
}

Common Mistakes

  1. Forgetting to allocate memory for the result matrix: Always allocate memory for the result matrix before performing the multiplication operation.
  2. Incorrect indexing: Ensure that you are using the correct indices (i, j, k) when accessing elements in both matrices and the result matrix.
  3. Not initializing the result matrix to zero: Initializing the result matrix to zero is crucial to avoid unexpected results due to residual values from previous operations.
  4. Leaking memory: Always deallocate memory for both matrices and the result matrix when finished to avoid memory leaks.
  5. Ignoring matrix dimensions: Ensure that the number of columns in the first matrix matches the number of rows in the second matrix for matrix multiplication to be possible.
  6. Transposing incorrectly: When transposing a matrix, swap its rows and columns correctly to achieve the desired result.
  7. Creating an identity matrix improperly: To create an identity matrix, set diagonal elements to 1 and all other elements to 0.

Practice Questions

  1. Multiply two 4x4 matrices manually using the technique demonstrated in this tutorial.
  2. Write a program that reads two matrices from the user, multiplies them, and prints the resulting matrix.
  3. Modify the example code to handle matrices with different numbers of rows or columns (square and non-square matrices).
  4. Implement transposition of a matrix using the same technique as matrix multiplication.
  5. Create an identity matrix of size 5x5 and print it to the console.
  6. Write a function that takes two matrices as input, performs their multiplication, and returns the resulting matrix.
  7. Modify the example code to handle matrices with complex elements (e.g., using a complex number data structure).
  8. Implement Strassen's algorithm for matrix multiplication and compare its performance with the manual method presented in this tutorial.

FAQ

  1. Why can't we use built-in functions for matrix multiplication? Manually implementing matrix multiplication allows us to understand the underlying memory management and performance implications, which is essential for debugging complex programs or tackling interview questions. Additionally, it provides a foundation for more advanced techniques like Strassen's algorithm.
  2. What happens if we try to multiply matrices with different numbers of rows and columns? In general, matrix multiplication requires that the number of columns in the first matrix matches the number of rows in the second matrix. If this condition isn't met, the operation is undefined. However, you can modify the example code to handle non-square matrices by adjusting the loops accordingly.
  3. Can we optimize the matrix multiplication code for better performance? Yes, there are several optimization techniques available for matrix multiplication, such as Strassen's algorithm and block matrix multiplication. These techniques reduce the time complexity of matrix multiplication and can be implemented in C++ for improved performance.
  4. What is the time complexity of the manual matrix multiplication code in this tutorial? The time complexity of the manual matrix multiplication code in this tutorial is O(n^3), where n represents the size of both matrices. This can be improved by using optimized algorithms like Strassen's algorithm, which reduces the time complexity to O(n^log2(7)).
  5. How does transposition affect the performance of matrix operations? Transposing a matrix can significantly impact the performance of certain matrix operations, such as solving linear systems or performing eigendecompositions. In some cases, it may be beneficial to transpose one or both matrices before performing an operation to reduce computation time or improve numerical stability. However, this depends on the specific problem and algorithm being used.
  6. What are some common applications of manual matrix multiplication in C++? Manual matrix multiplication can be useful in various areas, such as computer graphics (e.g., rendering transformations, lighting calculations), linear algebra (e.g., solving systems of linear equations, finding eigenvalues and eigenvectors), machine learning (e.g., training neural networks, implementing support vector machines), and physics simulations (e.g., simulating particle interactions, modeling electromagnetic fields).
  7. How can I handle matrices with complex elements in C++? To handle matrices with complex elements in C++, you can create a custom data structure for complex numbers and use it to represent the matrix elements. This allows you to perform arithmetic operations on the complex numbers and manipulate the matrix accordingly. You may also need to adjust the multiplication operation to account for the distributive property of complex numbers.
Example: Multiply two matrices without using functions (C++) | C++ | XQA Learn