Philip8
Philip8

Reputation: 67

My matrix multiplication code does nothing (C++)

My function template for matrix multiplication is as follows:

template<typename element_type>
void matMul(const std::vector<std::vector<element_type>> & mat1,
const std::vector<std::vector<element_type>> & mat2,
std::vector<std::vector<element_type>> & result
) {
if (mat1[0].size() != mat2.size()) {
    std::cout << "dimensions do not match..." << std::endl;
    return;
}

result.resize(mat1.size());
for (std::vector<double> & row : result) {
    row.resize(mat2[0].size());
}

for (unsigned int row_id = 0; row_id < mat1.size(); ++row_id) {
    for (unsigned int col_id = 0; col_id < mat2[0].size() < col_id; ++col_id) {
        for (unsigned int element_id = 0; element_id < mat1[0].size(); ++element_id) {
////////////////////////////////////////////////////////////////////////////////////
            result[row_id][col_id] += mat1[row_id][element_id] * mat2[element_id][col_id];//HERE I WILL MENTION BELOW...
////////////////////////////////////////////////////////////////////////////////////
        }
    }
}

I passed

std::vector<std::vector<double>> mul1 = {
    {1.0, 2.0, 3.0}, 
{4.0, 5.0, 6.0}
};

,

std::vector<std::vector<double>> mul2 = {
    {7.0, 8.0},
{9.0, 10.0}, 
{11.0, 12.0}
};

and

std::vector<std::vector<double>> result;

And the next code is for test:

matMul(mul1, mul2, result);
for (std::vector<double> row : result) {
    for (double element : row) {
        std::cout << element << " ";
    }
    std::cout << std::endl;
}

The output is:

0 0
0 0

When I tried to debug in Visual Studio 2017, I found the break point does not work in the place I mentioned above. It seemed to do exactly nothing and just to pass by the part. Why does my VS2017 ignore the part? And how to fix it?

Upvotes: 0

Views: 58

Answers (1)

Mike Vine
Mike Vine

Reputation: 9837

for (unsigned int col_id = 0; col_id < mat2[0].size() < col_id; ++col_id) {

Check your terminating condition. That doesn't seem right. You meant:

for (unsigned int col_id = 0; col_id < mat2[0].size(); ++col_id) {

Upvotes: 3

Related Questions