The following C++ program segment computes C = A * B, where A is an “m” by “n”
matrix, B is an “n” by “r” matrix and C is an “m” by “r” matrix.
for (long int i = 0; i < m; i++)
for (long int j = 0; j < r; j++)
{
long double Sum = 0;
for (long int k = 0; k < n; k++)
Sum = Sum + A[i][k] * B[k][j];
C[i][j] = Sum;
}
i) How many times does the code iterate in computing Sum?
ii) How many times does the code iterate in computing the j column of C, i.e. C[i][j] for a fixed i and j = 1, 2, 3, ..., r?
iii) How many times does the code iterate in computing the complete matrix C?
i) To compute a single value of Sum the code make n iterations
ii) To compute j-th column of C the code produces r*n intertions
iii) There are m*r*n total iterations in the complete calculation of the matrix C.
Comments
Leave a comment