I'm having trouble understanding what an "index of rows of a" in the problem is referring to... can you give me an example of that? Why does
sum = sum + a[(row * a_cols) + k] * b[k * b_cols + col]?
Thank you!
double dotProduct(double a[], const unsigned a_rows, const unsigned a_cols,
/* a is a matrix with a_rows rows and a_cols columns */
double b[], const unsigned b_cols,
/* b is also a matrix. It has a_cols rows and b_cols columns */
unsigned row, // an **index of a row of a**
unsigned col) // an index of a column of b
{
int k; // loop variable
double sum = 0.0; // the result of the dot product is stored here
k = 0;
while (k < a_cols) { // recall: a_cols == b_rows
/* we need to multiply a[row, k] and b[k, col] and add that to sum */
sum = sum + a[(row * a_cols) + k] * b[k * b_cols + col];
/* recall a[i,j] is stored at a[(i * a_cols) + j]
and b[i,j] is stored at b[(i * b_cols) + j] */
k += 1;
}
return sum;
}